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 e659c23bf2)
pull/83024/head
Felix Fontein 8 months ago committed by GitHub
parent daf71c51e0
commit 9b77492e96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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)."

@ -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:

@ -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

Loading…
Cancel
Save