Merge branch 'lineinfile_improvements' of git://github.com/leucos/ansible into devel

Conflicts:
	library/lineinfile
This commit is contained in:
Michael DeHaan 2013-02-23 12:45:36 -05:00
commit b739a5860a

View file

@ -87,15 +87,34 @@ 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_attrs(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):
@ -112,6 +131,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
lines = f.readlines() lines = f.readlines()
f.close() f.close()
msg = ""
mre = re.compile(regexp) mre = re.compile(regexp)
if not mre.search(line): if not mre.search(line):
module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp) module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)
@ -171,9 +192,18 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
f.writelines(lines) f.writelines(lines)
f.close() f.close()
[ msg, changed ] = check_file_attrs(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 +225,12 @@ 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_attrs(module, changed, msg)
module.exit_json(changed=changed, found=len(found), msg=msg)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -210,20 +245,22 @@ def main():
backup=dict(default=False, choices=BOOLEANS), backup=dict(default=False, choices=BOOLEANS),
), ),
mutually_exclusive = [['insertbefore', 'insertafter']], mutually_exclusive = [['insertbefore', 'insertafter']],
add_file_common_args=True,
supports_check_mode = 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>>