community.windows.win_lineinfile (2.2.0) — module

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

Authors: Brian Lloyd (@brianlloyd)

Install collection

Install with ansible-galaxy collection install community.windows:==2.2.0


Add to requirements.yml

  collections:
    - name: community.windows
      version: 2.2.0

Description

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.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Insert path without converting \r\n
  community.windows.win_lineinfile:
    path: c:\file.txt
    line: c:\return\new
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^name='
    line: 'name=JohnDoe'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^name='
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    regex: '^127\.0\.0\.1'
    line: '127.0.0.1 localhost'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- community.windows.win_lineinfile:
    path: C:\Temp\httpd.conf
    regex: '^Listen '
    insertafter: '^#Listen '
    line: Listen 8080
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- community.windows.win_lineinfile:
    path: C:\Temp\services
    regex: '^# port for http'
    insertbefore: '^www.*80/tcp'
    line: '# port for http by default'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create file if it doesn't exist with a specific encoding
  community.windows.win_lineinfile:
    path: C:\Temp\utf16.txt
    create: yes
    encoding: utf-16
    line: This is a utf-16 encoded file
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Update a line using backrefs
  community.windows.win_lineinfile:
    path: C:\Temp\example.conf
    backrefs: yes
    regex: '(^name=)'
    line: '$1JohnDoe'

Inputs

    
line:
    description:
    - Required for C(state=present). The line to insert/replace into the file. If C(backrefs)
      is set, may contain backreferences that will get expanded with the C(regexp) capture
      groups if the regexp matches.
    - 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.
    type: str

path:
    aliases:
    - dest
    - destfile
    - name
    description:
    - The path of the file to modify.
    - Note that the Windows path delimiter C(\) must be escaped as C(\\) when the line
      is double quoted.
    required: true
    type: path

regex:
    aliases:
    - regexp
    description:
    - The regular expression to look for in every line of the file. For C(state=present),
      the pattern to replace if found; only the last line found will be replaced. For
      C(state=absent), the pattern of the line to remove. Uses .NET compatible regular
      expressions; see U(https://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx).

state:
    choices:
    - absent
    - present
    default: present
    description:
    - Whether the line should be there or not.
    type: str

backup:
    default: false
    description:
    - Determine whether a backup should be created.
    - When set to C(yes), create a backup file including the timestamp information so
      you can get the original file back if you somehow clobbered it incorrectly.
    type: bool

create:
    default: false
    description:
    - Used with C(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.
    type: bool

newline:
    choices:
    - unix
    - windows
    default: windows
    description:
    - Specifies the line separator style to use for the modified file. This defaults to
      the windows line separator (C(\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.
    type: str

backrefs:
    default: false
    description:
    - Used with C(state=present). If set, line can contain backreferences (both positional
      and named) that will get populated if the C(regexp) matches. This flag changes the
      operation of the module slightly; C(insertbefore) and C(insertafter) will be ignored,
      and if the C(regexp) doesn't match anywhere in the file, the file will be left unchanged.
    - If the C(regexp) does match, the last matching line will be replaced by the expanded
      line parameter.
    type: bool

encoding:
    default: auto
    description:
    - Specifies the encoding of the source text file to operate on (and thus what the
      output encoding will be). The default of C(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 U(https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx).
    - This is mostly useful with C(create=yes) if you want to create a new file with a
      specific encoding. If C(create=yes) is specified without a specific encoding, the
      default encoding (UTF-8, no BOM) will be used.
    type: str

validate:
    description:
    - 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.
    type: str

insertafter:
    choices:
    - EOF
    - '*regex*'
    default: EOF
    description:
    - Used with C(state=present). If specified, the line will be inserted after the last
      match of specified regular expression. A special value is available; C(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 C(backrefs).
    type: str

insertbefore:
    choices:
    - BOF
    - '*regex*'
    description:
    - Used with C(state=present). If specified, the line will be inserted before the last
      match of specified regular expression. A value is available; C(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 C(backrefs).
    type: str

Outputs

backup:
  description:
  - Name of the backup file that was created.
  - This is now deprecated, use C(backup_file) instead.
  returned: if backup=yes
  sample: C:\Path\To\File.txt.11540.20150212-220915.bak
  type: str
backup_file:
  description: Name of the backup file that was created.
  returned: if backup=yes
  sample: C:\Path\To\File.txt.11540.20150212-220915.bak
  type: str

See also