From 39aba6d304e9295c4d850f4cbdd2e1d6e97cce0e Mon Sep 17 00:00:00 2001 From: Robert Osowiecki Date: Tue, 12 Nov 2019 02:36:16 +0100 Subject: [PATCH] lineinfile - don't run os.makedirs on empty dir path (#63921) (#64091) * Fix #63919: don't run os.makedirs on empty dir path * integration test for lineinfile create: yes without path (Sam Doran ) (cherry picked from commit 3c978a3225b1bb5118540dffd5006588f4228a8f) --- .../63919-lineinfile-create-no-dir-path.yml | 2 ++ lib/ansible/modules/files/lineinfile.py | 2 +- .../targets/lineinfile/tasks/main.yml | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/63919-lineinfile-create-no-dir-path.yml diff --git a/changelogs/fragments/63919-lineinfile-create-no-dir-path.yml b/changelogs/fragments/63919-lineinfile-create-no-dir-path.yml new file mode 100644 index 00000000000..05d236365fc --- /dev/null +++ b/changelogs/fragments/63919-lineinfile-create-no-dir-path.yml @@ -0,0 +1,2 @@ +bugfixes: + - "lineinfile - don't attempt mkdirs when path doesn't contain directory path" diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index 8187f71c5ba..568c28abfdf 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -259,7 +259,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) b_destpath = os.path.dirname(b_dest) - if not os.path.exists(b_destpath) and not module.check_mode: + if b_destpath and not os.path.exists(b_destpath) and not module.check_mode: try: os.makedirs(b_destpath) except Exception as e: diff --git a/test/integration/targets/lineinfile/tasks/main.yml b/test/integration/targets/lineinfile/tasks/main.yml index 819e82da9b5..840051cf02f 100644 --- a/test/integration/targets/lineinfile/tasks/main.yml +++ b/test/integration/targets/lineinfile/tasks/main.yml @@ -279,6 +279,24 @@ that: - "result.stat.checksum == '038f10f9e31202451b093163e81e06fbac0c6f3a'" +- name: Create a file without a path + lineinfile: + dest: file.txt + create: yes + line: Test line + register: create_no_path_test + +- name: Stat the file + stat: + path: file.txt + register: create_no_path_file + +- name: Ensure file was created + assert: + that: + - create_no_path_test is changed + - create_no_path_file.stat.exists + # Test EOF in cases where file has no newline at EOF - name: testnoeof deploy the file for lineinfile copy: