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:
Brian Coca 2020-11-09 14:45:09 -05:00 committed by GitHub
parent e7bf0696ef
commit a1730af91f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- blockinfile now returns name of backup file when this option is used.

View file

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

View file

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