diff --git a/changelogs/fragments/2923-archive-remove-bugfix.yml b/changelogs/fragments/2923-archive-remove-bugfix.yml new file mode 100644 index 0000000000..4bef5ef459 --- /dev/null +++ b/changelogs/fragments/2923-archive-remove-bugfix.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - archive - fixed task failure when using the ``remove`` option with a ``path`` containing nested files for + ``format``s other than ``zip`` (https://github.com/ansible-collections/community.general/issues/2919). diff --git a/plugins/modules/files/archive.py b/plugins/modules/files/archive.py index 5cdd6630d1..a2d3376613 100644 --- a/plugins/modules/files/archive.py +++ b/plugins/modules/files/archive.py @@ -399,13 +399,14 @@ class Archive(object): def remove_targets(self): for path in self.successes: - try: - if os.path.isdir(path): - shutil.rmtree(path) - else: - os.remove(path) - except OSError: - self.errors.append(_to_native(path)) + if os.path.exists(path): + try: + if os.path.isdir(path): + shutil.rmtree(path) + else: + os.remove(path) + except OSError: + self.errors.append(_to_native(path)) for path in self.paths: try: if os.path.isdir(path): diff --git a/tests/integration/targets/archive/tasks/remove.yml b/tests/integration/targets/archive/tasks/remove.yml index 9600eb9f6d..9f085e901a 100644 --- a/tests/integration/targets/archive/tasks/remove.yml +++ b/tests/integration/targets/archive/tasks/remove.yml @@ -148,7 +148,39 @@ - name: verify that excluded sub file is still present file: path={{ output_dir }}/tmpdir/sub/subfile.txt state=file -- name: remove temporary directory +- name: prep our files in tmpdir again + copy: src={{ item }} dest={{ output_dir }}/tmpdir/{{ item }} + with_items: + - foo.txt + - bar.txt + - empty.txt + - sub + - sub/subfile.txt + +- name: archive using gz and remove src directory + archive: + path: + - "{{ output_dir }}/tmpdir/" + dest: "{{ output_dir }}/archive_remove_05.gz" + format: gz + remove: yes + exclude_path: "{{ output_dir }}/tmpdir/sub/subfile.txt" + register: archive_remove_result_05 + +- name: verify that the files archived + file: path={{ output_dir }}/archive_remove_05.gz state=file + +- name: Verify source files were removed file: path: "{{ output_dir }}/tmpdir" state: absent + register: archive_source_file_removal_05 + +- name: Verify that task status is success + assert: + that: + - archive_remove_result_05 is success + - archive_source_file_removal_05 is not changed + +- name: remove our gz + file: path="{{ output_dir }}/archive_remove_05.gz" state=absent