lineinfile - Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression.
Synopsis
- This module will search a file for a line, and ensure that it is present or absent.
- This is primarily useful when you want to change a single line in a file only. See the replace module if you want to change multiple, similar lines or check blockinfile if you want to insert/update/remove a block of lines in a file. For other cases, see the copy or template modules.
Options
parameter | required | default | choices | comments |
---|---|---|---|---|
attributes (added in 2.3)
| no | None | Attributes the file or directory should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr. aliases: attr | |
backrefs | no | no |
| Used with state=present . If set, line can contain backreferences (both positional and named) that will get populated if the regexp matches. This flag changes the operation of the module slightly; insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file, the file will be left unchanged. If the regexp does match, the last matching line will be replaced by the expanded line parameter. |
backup | no | no |
| Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. |
create | no | no |
| Used with state=present . If specified, the file will be created if it does not already exist. By default it will fail if the file is missing. |
group | no | Name of the group that should own the file/directory, as would be fed to chown. | ||
insertafter | no | EOF |
| Used with state=present . If specified, the line will be inserted after the last match of specified regular expression. A special value is available; EOF for inserting the line at the end of the file. If specified regular expression has no matches, EOF will be used instead. May not be used with backrefs . |
insertbefore | no |
| Used with state=present . If specified, the line will be inserted before the last match of specified regular expression. A value is available; BOF for inserting the line at the beginning of the file. If specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with backrefs . | |
line | no | Required for state=present . The line to insert/replace into the file. If backrefs is set, may contain backreferences that will get expanded with the regexp capture groups if the regexp matches. | ||
mode | no | Mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers (like 0644). Leaving off the leading zero will likely have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r ). | ||
others | no | All arguments accepted by the file module also work here. | ||
owner | no | Name of the user that should own the file/directory, as would be fed to chown. | ||
path | yes | The file to modify. Before 2.3 this option was only usable as dest, destfile and name. aliases: dest, destfile, name | ||
regexp (added in 1.7)
| no | The regular expression to look for in every line of the file. For state=present , the pattern to replace if found. Only the last line found will be replaced. For state=absent , the pattern of the line(s) to remove. Uses Python regular expressions. See http://docs.python.org/2/library/re.html. | ||
selevel | no | s0 | Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the range . _default feature works as for seuser. | |
serole | no | Role part of SELinux file context, _default feature works as for seuser. | ||
setype | no | Type part of SELinux file context, _default feature works as for seuser. | ||
seuser | no | User part of SELinux file context. Will default to system policy, if applicable. If set to _default , it will use the user portion of the policy if available. | ||
state | no | present |
| Whether the line should be there or not. |
unsafe_writes (added in 2.2)
| no |
| Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, sometimes systems are configured or just broken in ways that prevent this. One example are docker mounted files, they cannot be updated atomically and can only be done in an unsafe manner. This boolean option allows ansible to fall back to unsafe methods of updating files for those cases in which you do not have any other choice. Be aware that this is subject to race conditions and can lead to data corruption. | |
validate | no | None | The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the example below. The command is passed securely so shell features like expansion and pipes won't work. |
Examples
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path' - lineinfile: path: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=enforcing' - lineinfile: path: /etc/sudoers state: absent regexp: '^%wheel' - lineinfile: path: /etc/hosts regexp: '^127\.0\.0\.1' line: '127.0.0.1 localhost' owner: root group: root mode: 0644 - lineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen ' insertafter: '^#Listen ' line: 'Listen 8080' - lineinfile: path: /etc/services regexp: '^# port for http' insertbefore: '^www.*80/tcp' line: '# port for http by default' # Add a line to a file if it does not exist, without passing regexp - lineinfile: path: /tmp/testfile line: '192.168.1.99 foo.lab.net foo' # Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs. - lineinfile: path: /etc/sudoers state: present regexp: '^%wheel\s' line: '%wheel ALL=(ALL) NOPASSWD: ALL' # Yaml requires escaping backslashes in double quotes but not in single quotes - lineinfile: path: /opt/jboss-as/bin/standalone.conf regexp: '^(.*)Xms(\\d+)m(.*)$' line: '\1Xms${xms}m\3' backrefs: yes # Validate the sudoers file before saving - lineinfile: path: /etc/sudoers state: present regexp: '^%ADMIN ALL=' line: '%ADMIN ALL=(ALL) NOPASSWD: ALL' validate: '/usr/sbin/visudo -cf %s'
Notes
Note
- As of Ansible 2.3, the dest option has been changed to path as default, but dest still works as well.
Status
This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.
Maintenance Info
For more information about Red Hat’s this support of this module, please refer to this knowledge base article<https://access.redhat.com/articles/rhel-top-support-policies>
For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.4/lineinfile_module.html