diff options
Diffstat (limited to 'files')
-rwxr-xr-x[-rw-r--r--] | files/blockinfile.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/files/blockinfile.py b/files/blockinfile.py index 81834dfd..38e719a9 100644..100755 --- a/files/blockinfile.py +++ b/files/blockinfile.py @@ -134,11 +134,11 @@ EXAMPLES = r""" marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->" content: "" -- name: insert/update "Match User" configuation block in /etc/ssh/sshd_config +- name: Add mappings to /etc/hosts blockinfile: dest: /etc/hosts block: | - {{item.name}} {{item.ip}} + {{item.ip}} {{item.name}} marker: "# {mark} ANSIBLE MANAGED BLOCK {{item.name}}" with_items: - { name: host1, ip: 10.10.1.10 } @@ -212,7 +212,8 @@ def main(): module.fail_json(rc=256, msg='Destination %s is a directory !' % dest) - if not os.path.exists(dest): + path_exists = os.path.exists(dest) + if not path_exists: if not module.boolean(params['create']): module.fail_json(rc=257, msg='Destination %s does not exist !' % dest) @@ -230,6 +231,9 @@ def main(): marker = params['marker'] present = params['state'] == 'present' + if not present and not path_exists: + module.exit_json(changed=False, msg="File not present") + if insertbefore is None and insertafter is None: insertafter = 'EOF' @@ -281,7 +285,7 @@ def main(): if lines: result = '\n'.join(lines) - if original.endswith('\n'): + if original and original.endswith('\n'): result += '\n' else: result = '' @@ -299,10 +303,13 @@ def main(): changed = True if changed and not module.check_mode: - if module.boolean(params['backup']) and os.path.exists(dest): + if module.boolean(params['backup']) and path_exists: module.backup_local(dest) write_changes(module, result, dest) + if module.check_mode and not path_exists: + module.exit_json(changed=changed, msg=msg) + msg, changed = check_file_attrs(module, changed, msg) module.exit_json(changed=changed, msg=msg) |