Ensure blockinfile correctly returns backupfile (#72544)
* Ensure blockinfile correctly returns backupfile Fixes #27626 based on #27859 Co-authored-by: Giovanni Sciortino (@giovannisciortino)
This commit is contained in:
parent
e7bf0696ef
commit
a1730af91f
3 changed files with 14 additions and 2 deletions
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- blockinfile now returns name of backup file when this option is used.
|
|
@ -328,9 +328,10 @@ def main():
|
|||
msg = 'Block inserted'
|
||||
changed = True
|
||||
|
||||
backup_file = None
|
||||
if changed and not module.check_mode:
|
||||
if module.boolean(params['backup']) and path_exists:
|
||||
module.backup_local(path)
|
||||
backup_file = module.backup_local(path)
|
||||
# We should always follow symlinks so that we change the real file
|
||||
real_path = os.path.realpath(params['path'])
|
||||
write_changes(module, result, real_path)
|
||||
|
@ -345,7 +346,11 @@ def main():
|
|||
attr_diff['after_header'] = '%s (file attributes)' % path
|
||||
|
||||
difflist = [diff, attr_diff]
|
||||
module.exit_json(changed=changed, msg=msg, diff=difflist)
|
||||
|
||||
if backup_file is None:
|
||||
module.exit_json(changed=changed, msg=msg, diff=difflist)
|
||||
else:
|
||||
module.exit_json(changed=changed, msg=msg, diff=difflist, backup_file=backup_file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -12,6 +12,11 @@
|
|||
backup: yes
|
||||
register: blockinfile_test0
|
||||
|
||||
- name: ensure we have a bcackup file
|
||||
assert:
|
||||
that:
|
||||
- "'backup_file' in blockinfile_test0"
|
||||
|
||||
- name: check content
|
||||
shell: 'grep -c -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ output_dir_test }}/sshd_config'
|
||||
register: blockinfile_test0_grep
|
||||
|
|
Loading…
Reference in a new issue