community.general/lib/ansible/modules/windows/win_lineinfile.py
Sam Doran c0c26f83a8 Examples syntax batch7 (#5624)
* Change example syntax on nxos_feature module

* Change example syntax on nxos_hsrp module

* Change example syntax on nxos_igmp module

* Change example syntax on nxos_interface module

* Change example syntax on nxos_interface_ospf module

* Change example syntax on nxos_ip_interface module

* Change example syntax on nxos_ping module

* Change example syntax on nxos_switchport module

* Change example syntax on nxos_vlan module

* Change example syntax on nxos_vrf module

* Change example syntax on nxos_vrf_interface module

* Change example syntax on nxos_vrrp module

* Change example syntax on meta module

* Change example syntax on set_fact module

* Change example syntax on win_copy module

* Change example syntax on win_file module

* Change example syntax on win_get_url module

Remove escaping of \ characeter in Windows paths since it's no longer required for single quoted or unquoted values when using multi-line YAML syntax.

* Change example syntax on win_lineinfile module

* Change example syntax on win_msi module

* Change example syntax on win_stat module

* Remove nxos_bgp example from nxos_igmp module

* Mark examples as regexp to avoid syntax error

* Cleanup win_copy.py examples

* Cleanup win_file.py examples

* Remove quotes in win_get_url.py examples

* Cleanup quotes and languare in win_lineinfile.py

* Cleanup examples in win_group.py

* Cleanup examples in win_service.py

* Don't use : in documentation because it breaks the YAML syntax check

* Cleanup win_copy.py examples

* Cleanup win_copy.py examples

* Minor change to fix test failure

* Use single quotes
2016-12-08 11:25:33 -05:00

163 lines
6.5 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = """
---
module: win_lineinfile
author: Brian Lloyd (brian.d.lloyd@gmail.com)
short_description: Ensure a particular line is in a file, or replace an
existing line using a back-referenced regular expression.
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.
version_added: "2.0"
options:
dest:
required: true
aliases: [ name, destfile ]
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.
regexp:
required: false
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:
required: false
choices: [ present, absent ]
default: "present"
description:
- Whether the line should be there or not.
line:
required: false
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.
backrefs:
required: false
default: "no"
choices: [ "yes", "no" ]
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.
insertafter:
required: false
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).
choices: [ 'EOF', '*regex*' ]
insertbefore:
required: false
version_added: "1.1"
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).
choices: [ 'BOF', '*regex*' ]
create:
required: false
choices: [ "yes", "no" ]
default: "no"
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.
backup:
required: false
default: "no"
choices: [ "yes", "no" ]
description:
- Create a backup file including the timestamp information so you can
get the original file back if you somehow clobbered it incorrectly.
validate:
required: false
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.
default: None
encoding:
required: false
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.
newline:
required: false
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."
choices: [ "windows", "unix" ]
default: "windows"
"""
EXAMPLES = r"""
- win_lineinfile:
dest: C:\temp\example.conf
regexp: '^name='
line: 'name=JohnDoe'
- win_lineinfile:
dest: C:\temp\example.conf
regexp: '^name='
state: absent
- win_lineinfile:
dest: C:\temp\example.conf
regexp: '^127\.0\.0\.1'
line: '127.0.0.1 localhost'
- win_lineinfile:
dest: C:\temp\httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: Listen 8080
- win_lineinfile:
dest: 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:
dest: 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:
dest: C:\temp\testfile.txt
line: Line added to file
newline: unix
"""