Improves lineinfile and adds file attr option

Added path expanding for dest in lineinfile
Added common file arguments to lineinfile so the module gets owner,
group, mode and SE options.
Decorated existing example to demonstate usage of file options and added
a couple more examples
Message is not set accordingly when file attributes were changed
'absent' handling code now handles the case where the file doesn't
exists (was issuing a Traceback before)
File attribute handling code has been added to the 'absent' handling
function too.
File attributes handling has been grouped in 'def check_file' since it's
required in both places.
'absent' mode now returns a message like it's counterpart 'present',
telling if file attributes were altered and if lines have matched.
reviewable/pr18780/r1
Michel Blanc 12 years ago
parent d5a236c53d
commit e64016f2af

@ -87,15 +87,33 @@ options:
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.
others:
description:
- All arguments accepted by the M(file) module also work here. If you
use file arguments with C(state=absent) and the file exists, it's perms,
ownership or SE linux context will be updated if needed.
required: false
examples: examples:
- code: 'lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled' - code: 'lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=disabled'
- code: 'lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"' - code: 'lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"'
- code: "lineinfile: dest=/etc/host regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644"
- code: 'lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"' - code: 'lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"'
- code: 'lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"' - code: 'lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"'
- code: "lineinfile: \\\"dest=/etc/sudoers state=present regexp='^%wheel' line ='%wheel ALL=(ALL) NOPASSWD: ALL'\\\"" - code: "lineinfile: \\\"dest=/etc/sudoers state=present regexp='^%wheel' line ='%wheel ALL=(ALL) NOPASSWD: ALL'\\\""
""" """
def check_file(module, changed, message=""):
file_args = module.load_file_common_arguments(module.params)
if module.set_file_attributes_if_different(file_args, False):
if changed:
message += " and "
changed = True
message += "ownership, perms or SE linux context changed"
return [ message, changed ]
def present(module, dest, regexp, line, insertafter, insertbefore, create, backup): def present(module, dest, regexp, line, insertafter, insertbefore, create, backup):
if os.path.isdir(dest): if os.path.isdir(dest):
@ -164,6 +182,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
msg = 'line added' msg = 'line added'
changed = True changed = True
file_args = module.load_file_common_arguments(module.params)
changed = module.set_file_attributes_if_different(file_args, changed)
if changed and not module.check_mode: if changed and not module.check_mode:
if backup and os.path.exists(dest): if backup and os.path.exists(dest):
module.backup_local(dest) module.backup_local(dest)
@ -171,9 +192,17 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
f.writelines(lines) f.writelines(lines)
f.close() f.close()
[ msg, changed ] = check_file(module, changed, msg)
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
def absent(module, dest, regexp, backup): def absent(module, dest, regexp, backup):
if os.path.isdir(dest):
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
elif not os.path.exists(dest):
module.exit_json(changed=False, msg="file not present")
msg = ""
f = open(dest, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
@ -195,7 +224,13 @@ def absent(module, dest, regexp, backup):
f = open(dest, 'wb') f = open(dest, 'wb')
f.writelines(lines) f.writelines(lines)
f.close() f.close()
module.exit_json(changed=changed, found=len(found))
if changed:
msg = "%s line(s) removed" % len(found)
[ msg, changed ] = check_file(module, changed, msg)
module.exit_json(changed=changed, found=len(found), msg=msg)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -209,21 +244,23 @@ def main():
create=dict(default=False, choices=BOOLEANS), create=dict(default=False, choices=BOOLEANS),
backup=dict(default=False, choices=BOOLEANS), backup=dict(default=False, choices=BOOLEANS),
), ),
mutually_exclusive = [['insertbefore', 'insertafter']], mutually_exclusive = [['insertbefore', 'insertafter']],
supports_check_mode = True add_file_common_args=True,
supports_check_mode = True
) )
params = module.params params = module.params
create = module.boolean(module.params.get('create', False)) create = module.boolean(module.params.get('create', False))
backup = module.boolean(module.params.get('backup', False)) backup = module.boolean(module.params.get('backup', False))
dest = os.path.expanduser(params['dest'])
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['dest'], params['regexp'], params['line'], present(module, dest, params['regexp'], params['line'],
params['insertafter'], params['insertbefore'], create, backup) params['insertafter'], params['insertbefore'], create, backup)
else: else:
absent(module, params['dest'], params['regexp'], backup) absent(module, 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>>

Loading…
Cancel
Save