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

backrefs

boolean

Used with regex. If set, the line is inserted/replaced only if the regex matches.

The matched groups can be referenced in line using backreferences.

Choices:

  • false ← (default)

  • true

create

boolean

Create the file if it does not exist.

Without this option, the task fails if the file does not exist.

Choices:

  • false ← (default)

  • true

follow

boolean

Whether to follow symlinks when setting file attributes.

When false, symlinks are not followed and attributes are set on the link itself (where supported).

When true, attributes are set on the symlink target.

Affects how mode, owner, and group are applied.

Choices:

  • false ← (default)

  • true

group

string

Group name that should own the file or directory.

Passed directly to the chgrp command.

If not specified, group ownership is not changed.

Not applied to symlinks when follow=false.

insertafter

string

Used with state=present.

If specified, the line is inserted after the last match of the specified regular expression.

Special values EOF for end of file and BOF for beginning of file.

insertbefore

string

Used with state=present.

If specified, the line is inserted before the last match of the specified regular expression.

Special value BOF for beginning of file.

line

aliases: value

string

The line to insert/replace into the file.

Required when state=present.

mode

string

Permissions for the file or directory.

Can be specified as an octal number (for example, '0644', '1777') or symbolic mode (like 'u+rwx').

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 umask or remain unchanged.

Not applied to symlinks when follow=false.

owner

string

User name that should own the file or directory.

Passed directly to the chown command.

If not specified, ownership is not changed.

Not applied to symlinks when follow=false.

path

aliases: dest, destfile, name

string / required

The file to modify.

regex

aliases: regexp

string

The regular expression to look for in every line of the file.

For state=present, the pattern to replace if found.

For state=absent, the pattern of the line(s) to remove.

state

string

Whether the line should be present or absent.

Choices:

  • "absent"

  • "present" ← (default)

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: full

Returns details on what has changed (or possibly needs changing in check_mode), when in diff mode.

platform

Platform: OpenWrt

Target platform for this module.

safe_file_operations

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'

Authors

  • Markus Weippert (@gekmihesg)