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 <sdoran@redhat.com>)

(cherry picked from commit 3c978a3225)
pull/64125/head
Robert Osowiecki 5 years ago committed by Matt Davis
parent 0e4fe7a52b
commit 39aba6d304

@ -0,0 +1,2 @@
bugfixes:
- "lineinfile - don't attempt mkdirs when path doesn't contain directory path"

@ -259,7 +259,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
if not create: if not create:
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest) module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
b_destpath = os.path.dirname(b_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: try:
os.makedirs(b_destpath) os.makedirs(b_destpath)
except Exception as e: except Exception as e:

@ -279,6 +279,24 @@
that: that:
- "result.stat.checksum == '038f10f9e31202451b093163e81e06fbac0c6f3a'" - "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 # Test EOF in cases where file has no newline at EOF
- name: testnoeof deploy the file for lineinfile - name: testnoeof deploy the file for lineinfile
copy: copy:

Loading…
Cancel
Save