2015-07-29 23:38:15 +00:00
|
|
|
#!/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
|
2016-11-22 16:07:21 +00:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-29 23:38:15 +00:00
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-11-22 16:07:21 +00:00
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2017-03-14 16:07:22 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2017-01-19 01:57:33 +00:00
|
|
|
DOCUMENTATION = r'''
|
2015-07-29 23:38:15 +00:00
|
|
|
---
|
|
|
|
module: win_lineinfile
|
2016-12-08 02:33:38 +00:00
|
|
|
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.
|
2015-07-29 23:38:15 +00:00
|
|
|
description:
|
|
|
|
- This module will search a file for a line, and ensure that it is present or absent.
|
2016-12-08 02:33:38 +00:00
|
|
|
- This is primarily useful when you want to change a single line in a file only.
|
2015-07-29 23:38:15 +00:00
|
|
|
version_added: "2.0"
|
|
|
|
options:
|
2017-01-03 12:47:00 +00:00
|
|
|
path:
|
2015-07-29 23:38:15 +00:00
|
|
|
required: true
|
2017-01-03 12:47:00 +00:00
|
|
|
aliases: [ dest, destfile, name ]
|
2015-07-29 23:38:15 +00:00
|
|
|
description:
|
|
|
|
- The path of the file to modify.
|
2016-11-22 16:07:21 +00:00
|
|
|
- Note that the Windows path delimiter C(\) must be escaped as C(\\) when the line is double quoted.
|
2017-01-03 12:47:00 +00:00
|
|
|
- Before 2.3 this option was only usable as I(dest), I(destfile) and I(name).
|
2015-07-29 23:38:15 +00:00
|
|
|
regexp:
|
|
|
|
required: false
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- >
|
|
|
|
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).
|
2015-07-29 23:38:15 +00:00
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: [ present, absent ]
|
|
|
|
default: "present"
|
|
|
|
description:
|
|
|
|
- Whether the line should be there or not.
|
|
|
|
line:
|
|
|
|
required: false
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- 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.
|
2015-07-29 23:38:15 +00:00
|
|
|
backrefs:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- 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.
|
2016-12-08 02:33:38 +00:00
|
|
|
- If the C(regexp) does match, the last matching line will be replaced by the expanded line parameter.
|
2015-07-29 23:38:15 +00:00
|
|
|
insertafter:
|
|
|
|
required: false
|
|
|
|
default: EOF
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- 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.
|
2016-11-22 16:07:21 +00:00
|
|
|
- If specified regular expression has no matches, EOF will be used instead. May not be used with C(backrefs).
|
2015-07-29 23:38:15 +00:00
|
|
|
choices: [ 'EOF', '*regex*' ]
|
|
|
|
insertbefore:
|
|
|
|
required: false
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- 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.
|
2016-11-22 16:07:21 +00:00
|
|
|
- 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).
|
2015-07-29 23:38:15 +00:00
|
|
|
choices: [ 'BOF', '*regex*' ]
|
|
|
|
create:
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
description:
|
2016-12-08 02:33:38 +00:00
|
|
|
- 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.
|
2015-07-29 23:38:15 +00:00
|
|
|
backup:
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
description:
|
2016-12-08 02:33:38 +00:00
|
|
|
- Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
|
2015-07-29 23:38:15 +00:00
|
|
|
validate:
|
|
|
|
required: false
|
|
|
|
description:
|
2016-11-22 16:07:21 +00:00
|
|
|
- Validation to run before copying into place. Use %s in the command to indicate the current file to validate.
|
2016-01-28 17:24:02 +00:00
|
|
|
- The command is passed securely so shell features like expansion and pipes won't work.
|
2015-07-29 23:38:15 +00:00
|
|
|
default: None
|
|
|
|
encoding:
|
|
|
|
required: false
|
|
|
|
default: "auto"
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- 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.
|
2015-07-29 23:38:15 +00:00
|
|
|
newline:
|
|
|
|
required: false
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- >
|
|
|
|
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.
|
2015-07-29 23:38:15 +00:00
|
|
|
choices: [ "windows", "unix" ]
|
|
|
|
default: "windows"
|
2017-01-03 12:47:00 +00:00
|
|
|
notes:
|
|
|
|
- As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but I(dest) still works as well.
|
2017-01-19 01:57:33 +00:00
|
|
|
'''
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2017-01-19 01:57:33 +00:00
|
|
|
EXAMPLES = r'''
|
2017-01-03 12:47:00 +00:00
|
|
|
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\example.conf
|
2016-11-22 16:07:21 +00:00
|
|
|
regexp: '^name='
|
|
|
|
line: 'name=JohnDoe'
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\example.conf
|
2016-11-22 16:07:21 +00:00
|
|
|
regexp: '^name='
|
|
|
|
state: absent
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\example.conf
|
2016-11-22 16:07:21 +00:00
|
|
|
regexp: '^127\.0\.0\.1'
|
|
|
|
line: '127.0.0.1 localhost'
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\httpd.conf
|
2016-11-22 16:07:21 +00:00
|
|
|
regexp: '^Listen '
|
|
|
|
insertafter: '^#Listen '
|
|
|
|
line: Listen 8080
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\services
|
2016-11-22 16:07:21 +00:00
|
|
|
regexp: '^# port for http'
|
|
|
|
insertbefore: '^www.*80/tcp'
|
|
|
|
line: '# port for http by default'
|
2015-07-29 23:38:15 +00:00
|
|
|
|
2016-10-13 14:47:50 +00:00
|
|
|
# Create file if it doesn't exist with a specific encoding
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\utf16.txt
|
2016-11-22 16:07:21 +00:00
|
|
|
create: yes
|
|
|
|
encoding: utf-16
|
|
|
|
line: This is a utf-16 encoded file
|
2015-07-29 23:38:15 +00:00
|
|
|
|
|
|
|
# Add a line to a file and ensure the resulting file uses unix line separators
|
2016-11-22 16:07:21 +00:00
|
|
|
- win_lineinfile:
|
2017-01-03 12:47:00 +00:00
|
|
|
path: C:\temp\testfile.txt
|
2016-11-22 16:07:21 +00:00
|
|
|
line: Line added to file
|
|
|
|
newline: unix
|
2017-02-16 01:09:30 +00:00
|
|
|
|
|
|
|
# Update a line using backrefs
|
|
|
|
- win_lineinfile:
|
|
|
|
path: C:\temp\example.conf
|
|
|
|
backrefs: yes
|
|
|
|
regexp: '(^name=)'
|
|
|
|
line: '$1JohnDoe'
|
2017-01-19 01:57:33 +00:00
|
|
|
'''
|