community.openwrt.lineinfile module – Manage lines in text files on OpenWrt targets
Note
This module is part of the community.openwrt collection (version 0.5.0).
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.openwrt.
To use it in a playbook, specify: community.openwrt.lineinfile.
Synopsis
The community.openwrt.lineinfile module ensures a particular line is in a file, or replaces an existing line using a regular expression.
This is useful when you want to change a single line in a file.
Note
This module has a corresponding action plugin.
Parameters
Parameter |
Comments |
|---|---|
Create the file if it does not exist. Without this option, the task fails if the file does not exist. Choices:
|
|
Group name that should own the file or directory. Passed directly to the If not specified, group ownership is not changed. Not applied to symlinks when |
|
Used with If specified, the line is inserted after the last match of the specified regular expression. Special values |
|
Used with If specified, the line is inserted before the last match of the specified regular expression. Special value |
|
The line to insert/replace into the file. Required when |
|
Permissions for the file or directory. Can be specified as an octal number (for example, When using octal notation, quote the value to ensure it is treated as a string. If not specified, permissions may be determined by the system default Not applied to symlinks when |
|
User name that should own the file or directory. Passed directly to the If not specified, ownership is not changed. Not applied to symlinks when |
|
The file to modify. |
|
The regular expression to look for in every line of the file. For For |
|
Whether the line should be present or absent. Choices:
|
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: full |
Can run in |
|
Support: full |
Returns details on what has changed (or possibly needs changing in |
|
Platform: OpenWrt |
Target platform for this module. |
|
Support: none This module uses basic shell commands for file operations and does not implement Ansible’s atomic file operation functions. |
Uses Ansible’s strict file operation functions to ensure proper permissions and avoid data corruption. |
Examples
- name: Ensure a line is present in a file
community.openwrt.lineinfile:
path: /etc/config/network
line: "option dns '8.8.8.8'"
- name: Replace a line matching a pattern
community.openwrt.lineinfile:
path: /etc/config/system
regex: '^\s*option hostname'
line: "option hostname 'newname'"
- name: Remove lines matching a pattern
community.openwrt.lineinfile:
path: /etc/config/firewall
regex: '^\s*option dest'
state: absent
- name: Insert line after match
community.openwrt.lineinfile:
path: /etc/hosts
line: 192.168.1.100 myhost
insertafter: '^127.0.0.1'