community.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 community.windows collection (version 2.4.0).
You might already have this collection installed if you are using the ansible package.
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.windows.
To use it in a playbook, specify: community.windows.win_lineinfile.
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 | Comments | 
|---|---|
| Used with  If the  Choices: 
 | |
| Determine whether a backup should be created. When set to  Choices: 
 | |
| Used with  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 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  Default:  | |
| Used with  If specified regular expression has no matches, EOF will be used instead. May not be used with  Choices: 
 | |
| 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  Choices: 
 | |
| 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. | |
| Specifies the line separator style to use for the modified file. This defaults to the windows line separator ( 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  | |
| Whether the line should be there or not. Choices: 
 | |
| 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. | 
See Also
See also
- ansible.builtin.assemble
- Assemble configuration files from fragments. 
- ansible.builtin.lineinfile
- Manage lines in text files. 
Examples
- name: Insert path without converting \r\n
  community.windows.win_lineinfile:
    path: c:\file.txt
    line: c:\return\new
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^name='
    line: 'name=JohnDoe'
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^name='
    state: absent
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^127\.0\.0\.1'
    line: '127.0.0.1 localhost'
- community.windows.win_lineinfile:
    path: C:\Temp\httpd.conf
    regex: '^Listen '
    insertafter: '^#Listen '
    line: Listen 8080
- community.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
  community.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
  community.windows.win_lineinfile:
    path: C:\Temp\testfile.txt
    line: Line added to file
    newline: unix
- name: Update a line using backrefs
  community.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. This is now deprecated, use  Returned: if backup=yes Sample:  | |
| Name of the backup file that was created. Returned: if backup=yes Sample:  | 
