From 9b77492e96106b616f2c23e147357b323b97b265 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 10 Apr 2024 21:27:50 +0200 Subject: [PATCH] blockinfile: do not crash when filename has no path (#81638) (#82869) * Do not crash when filename has no path. * Clean up file after test. (cherry picked from commit e659c23bf23fa4d6c3c39de1e781746a811dae03) --- 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 8c83bf0bdd9..3ede6fd59f9 100644 --- a/lib/ansible/modules/blockinfile.py +++ b/lib/ansible/modules/blockinfile.py @@ -269,7 +269,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