win_lineinfile – Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression
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.
Parameters
Parameter | Choices/Defaults | Comments |
---|---|---|
backrefs boolean |
|
Used with If the |
backup boolean |
|
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly. |
create boolean |
|
Used with |
encoding - |
Default: "auto" |
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 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 |
insertafter - |
|
Used with If specified regular expression has no matches, EOF will be used instead. May not be used with |
insertbefore - |
|
Used with If specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with |
line - |
Required for 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 '\r\n', expanded. To print such characters literally, use single or no quotes. | |
newline - |
|
Specifies the line separator style to use for the modified file. This defaults to the windows line separator ( |
path path / required |
The path of the file to modify. Note that the Windows path delimiter Before 2.3 this option was only usable as dest, destfile and name.
| |
regexp - |
The regular expression to look for in every line of the file. For | |
state - |
|
Whether the line should be there or not. |
validate - |
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. |
Notes
Note
- As of Ansible 2.3, the dest option has been changed to path as default, but dest still works as well.
Examples
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- name: insert path without converting \r\n
win_lineinfile:
path: c:\file.txt
line: c:\return\new
- 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'
Status
- This module is not guaranteed to have a backwards compatible interface. [preview]
- This module is maintained by the Ansible Community. [community]
Authors
- Brian Lloyd (@brianlloyd)
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.7/modules/win_lineinfile_module.html