查找并取消归档 Ansible 模块

Find and unarchive Ansible module

如何将以下 Ansible 任务修复到 find 文件和 unarchive

- name: find the file
  find:
    paths: /home/ec2-user/aa/
    pattern: "file-*.tar.gz"
  register: copied_file

- name: extract  the file
  unarchive:
     src: /home/ec2-user/aa/"{{ item }}"
     dest: /home/ec2-user/bb/
  with_fileglob:
      - "{{ copied_file.files }}"

我遇到了这个错误:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'list' object has no attribute 'rfind'
fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

list' object has no attribute 'rfind'

您的情况存在数据结构不匹配问题。

- name: Find file
  find:
    paths: "/home/{{ ansible_user }}"
    pattern: "*.tar.gz"
  register: result

Ansible find_module will return as value 不仅是文件名,还有一个列表。然后需要遍历它以获取文件路径。

- name: Show result file path
  debug:
    msg: "{{ item.path }}"
  loop: "{{ result.files }}"

因此可能适合你

- name: Extract file
  unarchive:
    src: {{ item.path }}
    dest: /home/{{ ansible_user }}/extracted/
  loop: {{ result.files }}