diff --git a/changelogs/fragments/57618-fix-diff-on-absent-files.yaml b/changelogs/fragments/57618-fix-diff-on-absent-files.yaml new file mode 100644 index 00000000000..a45790319a0 --- /dev/null +++ b/changelogs/fragments/57618-fix-diff-on-absent-files.yaml @@ -0,0 +1,2 @@ +bugfixes: + - "Fix --diff to produce output when creating a new file (https://github.com/ansible/ansible/issues/57618)" diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 2f6237d007c..f633c5c2293 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -1104,7 +1104,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): if not peek_result.get('failed', False) or peek_result.get('rc', 0) == 0: - if peek_result.get('state') == 'absent': + if peek_result.get('state') in (None, 'absent'): diff['before'] = u'' elif peek_result.get('appears_binary'): diff['dst_binary'] = 1 diff --git a/test/integration/targets/copy/tasks/main.yml b/test/integration/targets/copy/tasks/main.yml index c0a48e314ba..023d872183d 100644 --- a/test/integration/targets/copy/tasks/main.yml +++ b/test/integration/targets/copy/tasks/main.yml @@ -61,6 +61,19 @@ - import_tasks: acls.yml when: ansible_system == 'Linux' + # https://github.com/ansible/ansible/issues/57618 + - name: Test diff contents + copy: + content: 'Ansible managed\n' + dest: "{{ local_temp_dir }}/file.txt" + diff: yes + register: diff_output + + - assert: + that: + - 'diff_output.diff[0].before == ""' + - '"Ansible managed" in diff_output.diff[0].after' + always: - name: Cleaning file: