ansible.windows.win_lineinfile module – Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression
Note
This module is part of the ansible.windows collection (version 3.9.1).
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 ansible.windows.
To use it in a playbook, specify: ansible.windows.win_lineinfile.
New in ansible.windows 3.9.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.
For non-Windows targets, use the ansible.builtin.lineinfile module instead.
Parameters
Parameter |
Comments |
|---|---|
Used with If set, This flag changes the operation of the module slightly; If the Choices:
|
|
Determine whether a backup should be created. When set to Choices:
|
|
Used with If specified, the file will be created if it does not already exist. By default it will fail if the file is missing. Choices:
|
|
Specifies the encoding of the source text file to operate on (and thus what the output encoding will be). The default of An explicit encoding can be passed as a string that is a valid value to pass to the .NET framework This is mostly useful with If Default: |
|
Used with If specified, the line will be inserted after the last match of the specified regular expression. A special value If the specified regular expression has no matches, May not be used with Defaults to |
|
Used with If specified, the line will be inserted before the last match of the specified regular expression. A special value If the specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with |
|
Required for If Be aware that the line is processed first on the controller and thus is dependent on yaml quoting rules. Any double quoted line will have control characters, such as |
|
Specifies the line separator style to use for the modified file. This defaults to the windows line separator Note that the indicated line separator will be used for file output regardless of the original line separator that appears in the input file. Choices:
|
|
The path of the file to modify. Note that the Windows path delimiter |
|
The regular expression to look for in every line of the file. For For Uses .NET compatible regular expressions, see https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference. |
|
Whether the line should be there or not. Choices:
|
|
Validation to run before copying into place. Use The command is passed securely so shell features like expansion and pipes will not work. |
See Also
See also
- ansible.builtin.assemble
The official documentation on the ansible.builtin.assemble module.
- ansible.builtin.lineinfile
The official documentation on the ansible.builtin.lineinfile module.
- ansible.windows.win_copy
Copies files to remote locations on windows hosts.
- ansible.windows.win_template
Template a file out to a remote server.
Examples
- name: Insert path without converting \r\n
ansible.windows.win_lineinfile:
path: c:\file.txt
line: c:\return\new
- name: Replace a line matching a regex
ansible.windows.win_lineinfile:
path: C:\Temp\example.conf
regex: '^name='
line: 'name=JohnDoe'
- name: Remove a line matching a regex
ansible.windows.win_lineinfile:
path: C:\Temp\example.conf
regex: '^name='
state: absent
- name: Ensure the localhost entry is correct
ansible.windows.win_lineinfile:
path: C:\Temp\example.conf
regex: '^127\.0\.0\.1'
line: '127.0.0.1 localhost'
- name: Uncomment and set a Listen directive
ansible.windows.win_lineinfile:
path: C:\Temp\httpd.conf
regex: '^Listen '
insertafter: '^#Listen '
line: Listen 8080
- name: Insert a comment before a services entry
ansible.windows.win_lineinfile:
path: C:\Temp\services
regex: '^# port for http'
insertbefore: '^www.*80/tcp'
line: '# port for http by default'
- name: Create file if it doesn't exist with a specific encoding
ansible.windows.win_lineinfile:
path: C:\Temp\utf16.txt
create: true
encoding: utf-16
line: This is a utf-16 encoded file
- name: Add a line to a file and ensure the resulting file uses unix line separators
ansible.windows.win_lineinfile:
path: C:\Temp\testfile.txt
line: Line added to file
newline: unix
- name: Update a line using backrefs
ansible.windows.win_lineinfile:
path: C:\Temp\example.conf
backrefs: true
regex: '(^name=)'
line: '$1JohnDoe'
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
Name of the backup file that was created. Returned: if Sample: |
|
The encoding that was used to read from and write to the file. Returned: success Sample: |
|
A short description of the change that was made. Returned: success Sample: |