From e659c23bf23fa4d6c3c39de1e781746a811dae03 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 21 Mar 2024 00:39:19 +0100 Subject: [PATCH] blockinfile: do not crash when filename has no path (#81638) * Do not crash when filename has no path. * Clean up file after test. --- changelogs/fragments/81638-blockinfile.yml | 2 ++ lib/ansible/modules/blockinfile.py | 2 +- .../targets/blockinfile/tasks/create_file.yml | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/81638-blockinfile.yml diff --git a/changelogs/fragments/81638-blockinfile.yml b/changelogs/fragments/81638-blockinfile.yml new file mode 100644 index 00000000000..ecc154fab80 --- /dev/null +++ b/changelogs/fragments/81638-blockinfile.yml @@ -0,0 +1,2 @@ +bugfixes: + - "blockinfile - when ``create=true`` is used with a filename without path, the module crashed (https://github.com/ansible/ansible/pull/81638)." diff --git a/lib/ansible/modules/blockinfile.py b/lib/ansible/modules/blockinfile.py index 4da042bbd4a..6d32e4de30d 100644 --- a/lib/ansible/modules/blockinfile.py +++ b/lib/ansible/modules/blockinfile.py @@ -268,7 +268,7 @@ def main(): 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: + if destpath and not os.path.exists(destpath) and not module.check_mode: try: os.makedirs(destpath) except OSError as e: diff --git a/test/integration/targets/blockinfile/tasks/create_file.yml b/test/integration/targets/blockinfile/tasks/create_file.yml index c8ded3000b3..9a5cf05fbba 100644 --- a/test/integration/targets/blockinfile/tasks/create_file.yml +++ b/test/integration/targets/blockinfile/tasks/create_file.yml @@ -30,3 +30,17 @@ - empty_test_2 is changed - "'Block removed' in empty_test_2.msg" - empty_test_stat.stat.size == 0 + +- block: + - name: Create file in current directory + blockinfile: + path: "empty.txt" + block: Hello. + state: present + create: yes + + always: + - name: Remove file + file: + path: "empty.txt" + state: absent