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

Merge pull request #1237 from dagwieers/lineinfile-alias

Use the standard 'dest' option for target files, make 'name' an alias
This commit is contained in:
Michael DeHaan 2012-10-05 05:09:36 -07:00
commit 9f1690ab64

View file

@ -32,8 +32,9 @@ description:
file only. For other cases, see the M(copy) or M(template) modules. file only. For other cases, see the M(copy) or M(template) modules.
version_added: "0.7" version_added: "0.7"
options: options:
name: dest:
required: true required: true
aliases: [ name, destfile ]
description: description:
- The file to modify - The file to modify
regexp: regexp:
@ -71,12 +72,12 @@ options:
- 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.
examples: examples:
- code: lineinfile name=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled - code: lineinfile dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled
- code: lineinfile name=/etc/sudoers state=absent regexp="^%wheel" - code: lineinfile dest=/etc/sudoers state=absent regexp="^%wheel"
''' '''
def present(module, name, regexp, line, insertafter, backup): def present(module, dest, regexp, line, insertafter, backup):
f = open(name, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
@ -124,15 +125,15 @@ def present(module, name, regexp, line, insertafter, backup):
if changed: if changed:
if backup: if backup:
module.backup_local(name) module.backup_local(dest)
f = open(name, 'wb') f = open(dest, 'wb')
f.writelines(lines) f.writelines(lines)
f.close() f.close()
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
def absent(module, name, regexp, backup): def absent(module, dest, regexp, backup):
f = open(name, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
cre = re.compile(regexp) cre = re.compile(regexp)
@ -147,8 +148,8 @@ def absent(module, name, regexp, backup):
changed = len(found) > 0 changed = len(found) > 0
if changed: if changed:
if backup: if backup:
module.backup_local(name) module.backup_local(dest)
f = open(name, 'wb') f = open(dest, 'wb')
f.writelines(lines) f.writelines(lines)
f.close() f.close()
module.exit_json(changed=changed, found=len(found)) module.exit_json(changed=changed, found=len(found))
@ -156,7 +157,7 @@ def absent(module, name, regexp, backup):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
name=dict(required=True, aliases=['dest', 'destfile']), dest=dict(required=True, aliases=['name', 'destfile']),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
regexp=dict(required=True), regexp=dict(required=True),
line=dict(aliases=['value']), line=dict(aliases=['value']),
@ -171,10 +172,10 @@ def main():
if params['state'] == 'present': if params['state'] == 'present':
if 'line' not in params: if 'line' not in params:
module.fail_json(msg='line= is required with state=present') module.fail_json(msg='line= is required with state=present')
present(module, params['name'], params['regexp'], params['line'], present(module, params['dest'], params['regexp'], params['line'],
params['insertafter'], backup) params['insertafter'], backup)
else: else:
absent(module, params['name'], params['regexp'], backup) absent(module, params['dest'], params['regexp'], backup)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>