From 60ed80f99aae1826e1d17a989f86c84b10b6802f Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Sun, 14 Dec 2014 20:48:36 -0500 Subject: [PATCH] Fix breakage in lineinfile check mode when target file does not exist. Similarly to https://github.com/ansible/ansible/issues/6182, checking of the file attributes should be avoided in check mode when the file didn't originally exist. Also, avoid creating parent directories in check mode. Fixes https://github.com/ansible/ansible/issues/9546 --- lib/ansible/modules/files/lineinfile.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index ef73bde7b7..b9fc628e10 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -192,7 +192,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, if not create: module.fail_json(rc=257, msg='Destination %s does not exist !' % dest) destpath = os.path.dirname(dest) - if not os.path.exists(destpath): + if not os.path.exists(destpath) and not module.check_mode: os.makedirs(destpath) lines = [] else: @@ -282,6 +282,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backupdest = module.backup_local(dest) write_changes(module, lines, dest) + if module.check_mode and not os.path.exists(dest): + module.exit_json(changed=changed, msg=msg, backup=backupdest) + msg, changed = check_file_attrs(module, changed, msg) module.exit_json(changed=changed, msg=msg, backup=backupdest)