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