Documentation

win_lineinfile - Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression.

New in version 2.0.

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.

Options

parameter required default choices comments
backrefs
no no
  • yes
  • 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
  • yes
  • 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
  • yes
  • 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.
encoding
no auto
Specifies the encoding of the source text file to operate on (and thus what the output encoding will be). The default of auto will cause the module to auto-detect the encoding of the source file and ensure that the modified file is written with the same encoding.
An explicit encoding can be passed as a string that is a valid value to pass to the .NET framework System.Text.Encoding.GetEncoding() method - see https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx.
This is mostly useful with create=yes if you want to create a new file with a specific encoding. If create=yes is specified without a specific encoding, the default encoding (UTF-8, no BOM) will be used.
insertafter
no EOF
  • EOF
  • *regex*
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
  • BOF
  • *regex*
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.
newline
no windows
  • windows
  • unix
Specifies the line separator style to use for the modified file. This defaults to the windows line separator (\r\n). Note that the indicated line separator will be used for file output regardless of the original line separator that appears in the input file.
path
yes
The path of the file to modify.
Note that the Windows path delimiter \ must be escaped as \\ when the line is double quoted.
Before 2.3 this option was only usable as dest, destfile and name.

aliases: dest, destfile, name
regexp
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 to remove. Uses .NET compatible regular expressions; see https://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx.
state
no present
  • present
  • absent
Whether the line should be there or not.
validate
no None
Validation to run before copying into place. Use %s in the command to indicate the current file to validate.
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'
- win_lineinfile:
    path: C:\temp\example.conf
    regexp: '^name='
    line: 'name=JohnDoe'

- win_lineinfile:
    path: C:\temp\example.conf
    regexp: '^name='
    state: absent

- win_lineinfile:
    path: C:\temp\example.conf
    regexp: '^127\.0\.0\.1'
    line: '127.0.0.1 localhost'

- win_lineinfile:
    path: C:\temp\httpd.conf
    regexp: '^Listen '
    insertafter: '^#Listen '
    line: Listen 8080

- win_lineinfile:
    path: C:\temp\services
    regexp: '^# port for http'
    insertbefore: '^www.*80/tcp'
    line: '# port for http by default'

# Create file if it doesn't exist with a specific encoding
- win_lineinfile:
    path: C:\temp\utf16.txt
    create: yes
    encoding: utf-16
    line: This is a utf-16 encoded file

# Add a line to a file and ensure the resulting file uses unix line separators
- win_lineinfile:
    path: C:\temp\testfile.txt
    line: Line added to file
    newline: unix

# Update a line using backrefs
- win_lineinfile:
    path: C:\temp\example.conf
    backrefs: yes
    regexp: '(^name=)'
    line: '$1JohnDoe'

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.

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.