From fb2c0181c505fa7387bd397137b195a8c646adef Mon Sep 17 00:00:00 2001 From: Giovanni Sciortino <30353557+giovannisciortino@users.noreply.github.com> Date: Tue, 6 Feb 2018 16:48:15 +0100 Subject: [PATCH] Fixes: #20986 blockinfile create undelying directory when create=true (#27961) --- lib/ansible/modules/files/blockinfile.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ansible/modules/files/blockinfile.py b/lib/ansible/modules/files/blockinfile.py index 654fed59d9..0b69abc63e 100644 --- a/lib/ansible/modules/files/blockinfile.py +++ b/lib/ansible/modules/files/blockinfile.py @@ -223,6 +223,12 @@ def main(): if not module.boolean(params['create']): module.fail_json(rc=257, msg='Path %s does not exist !' % path) + destpath = os.path.dirname(path) + if not os.path.exists(destpath) and not module.check_mode: + try: + os.makedirs(destpath) + except Exception as e: + module.fail_json(msg='Error creating %s Error code: %s Error description: %s' % (destpath, e[0], e[1])) original = None lines = [] else: