1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

lineinfile: pylint fixes and docs (#30854)

This PR includes;
- pylint fixes
- Documentation updates
This commit is contained in:
Dag Wieers 2017-10-30 00:55:14 +01:00 committed by GitHub
parent 2f8c88b687
commit babc2354a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,15 +18,15 @@ DOCUMENTATION = """
--- ---
module: lineinfile module: lineinfile
author: author:
- "Daniel Hokka Zakrissoni (@dhozac)" - Daniel Hokka Zakrissoni (@dhozac)
- "Ahti Kitsik (@ahtik)" - Ahti Kitsik (@ahtik)
extends_documentation_fragment: extends_documentation_fragment:
- files - files
- validate - validate
short_description: Ensure a particular line is in a file, or replace an short_description: Manage lines in text files
existing line using a back-referenced regular expression.
description: description:
- This module will search a file for a line, and ensure that it is present or absent. - This module ensures a particular line is in a file, or replace an
existing line using a back-referenced regular expression.
- This is primarily useful when you want to change a single line in - This is primarily useful when you want to change a single line in
a file only. See the M(replace) module if you want to change a file only. See the M(replace) module if you want to change
multiple, similar lines or check M(blockinfile) if you want to insert/update/remove a block of lines in a file. multiple, similar lines or check M(blockinfile) if you want to insert/update/remove a block of lines in a file.
@ -34,38 +34,30 @@ description:
version_added: "0.7" version_added: "0.7"
options: options:
path: path:
required: true
aliases: [ 'dest', 'destfile', 'name' ]
description: description:
- The file to modify. - The file to modify.
- Before 2.3 this option was only usable as I(dest), I(destfile) and I(name). - Before 2.3 this option was only usable as I(dest), I(destfile) and I(name).
aliases: [ dest, destfile, name ]
required: true
regexp: regexp:
required: false
version_added: 1.7
description: description:
- The regular expression to look for in every line of the file. For - 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 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(s) found will be replaced. For C(state=absent), the pattern of the line(s)
to remove. Uses Python regular expressions; see to remove. Uses Python regular expressions; see
U(http://docs.python.org/2/library/re.html). U(http://docs.python.org/2/library/re.html).
version_added: '1.7'
state: state:
required: false
choices: [ present, absent ]
default: "present"
aliases: []
description: description:
- Whether the line should be there or not. - Whether the line should be there or not.
choices: [ absent, present ]
default: present
line: line:
required: false
description: description:
- Required for C(state=present). The line to insert/replace into the - Required for C(state=present). The line to insert/replace into the
file. If C(backrefs) is set, may contain backreferences that will get file. If C(backrefs) is set, may contain backreferences that will get
expanded with the C(regexp) capture groups if the regexp matches. expanded with the C(regexp) capture groups if the regexp matches.
backrefs: backrefs:
required: false
default: "no"
choices: [ "yes", "no" ]
version_added: "1.1"
description: description:
- Used with C(state=present). If set, line can contain backreferences - Used with C(state=present). If set, line can contain backreferences
(both positional and named) that will get populated if the C(regexp) (both positional and named) that will get populated if the C(regexp)
@ -74,45 +66,43 @@ options:
doesn't match anywhere in the file, the file will be left unchanged. 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 If the C(regexp) does match, the last matching line will be replaced by
the expanded line parameter. the expanded line parameter.
type: bool
default: 'no'
version_added: "1.1"
insertafter: insertafter:
required: false
default: EOF
description: description:
- Used with C(state=present). If specified, the line will be inserted - Used with C(state=present). If specified, the line will be inserted
after the last match of specified regular expression. A special value is 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. 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. If specified regular expression has no matches, EOF will be used instead.
May not be used with C(backrefs). May not be used with C(backrefs).
choices: [ 'EOF', '*regex*' ] choices: [ EOF, '*regex*' ]
default: EOF
insertbefore: insertbefore:
required: false
version_added: "1.1"
description: description:
- Used with C(state=present). If specified, the line will be inserted - Used with C(state=present). If specified, the line will be inserted
before the last match of specified regular expression. A value is before the last match of specified regular expression. A value is
available; C(BOF) for inserting the line at the beginning of the file. available; C(BOF) for inserting the line at the beginning of the file.
If specified regular expression has no matches, the line will be 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). inserted at the end of the file. May not be used with C(backrefs).
choices: [ 'BOF', '*regex*' ] choices: [ BOF, '*regex*' ]
version_added: "1.1"
create: create:
required: false
choices: [ "yes", "no" ]
default: "no"
description: description:
- Used with C(state=present). If specified, the file will be created - 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 if it does not already exist. By default it will fail if the file
is missing. is missing.
type: bool
default: 'no'
backup: backup:
required: false
default: "no"
choices: [ "yes", "no" ]
description: description:
- Create a backup file including the timestamp information so you can - Create a backup file including the timestamp information so you can
get the original file back if you somehow clobbered it incorrectly. get the original file back if you somehow clobbered it incorrectly.
type: bool
default: 'no'
others: others:
description: description:
- All arguments accepted by the M(file) module also work here. - All arguments accepted by the M(file) module also work here.
required: false
notes: 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. - As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but I(dest) still works as well.
""" """
@ -177,8 +167,8 @@ EXAMPLES = r"""
validate: '/usr/sbin/visudo -cf %s' validate: '/usr/sbin/visudo -cf %s'
""" """
import re
import os import os
import re
import tempfile import tempfile
# import module snippets # import module snippets
@ -311,7 +301,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
elif insertafter == 'EOF' or index[1] == -1: elif insertafter == 'EOF' or index[1] == -1:
# If the file is not empty then ensure there's a newline before the added line # If the file is not empty then ensure there's a newline before the added line
if len(b_lines) > 0 and not b_lines[-1][-1:] in (b('\n'), b('\r')): if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')):
b_lines.append(b_linesep) b_lines.append(b_linesep)
b_lines.append(b_line + b_linesep) b_lines.append(b_line + b_linesep)
@ -408,20 +398,20 @@ def absent(module, dest, regexp, line, backup):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
path=dict(required=True, aliases=['dest', 'destfile', 'name'], type='path'), path=dict(type='path', required=True, aliases=['dest', 'destfile', 'name']),
state=dict(default='present', choices=['absent', 'present']), state=dict(type='str', default='present', choices=['absent', 'present']),
regexp=dict(default=None), regexp=dict(type='str'),
line=dict(aliases=['value']), line=dict(type='str', aliases=['value']),
insertafter=dict(default=None), insertafter=dict(type='str'),
insertbefore=dict(default=None), insertbefore=dict(type='str'),
backrefs=dict(default=False, type='bool'), backrefs=dict(type='bool', default=False),
create=dict(default=False, type='bool'), create=dict(type='bool', default=False),
backup=dict(default=False, type='bool'), backup=dict(type='bool', default=False),
validate=dict(default=None, type='str'), validate=dict(type='str'),
), ),
mutually_exclusive=[['insertbefore', 'insertafter']], mutually_exclusive=[['insertbefore', 'insertafter']],
add_file_common_args=True, add_file_common_args=True,
supports_check_mode=True supports_check_mode=True,
) )
params = module.params params = module.params