0

I tried below ansible tasks with copy and unarchive modules and facing error

- name: find and archive
  hosts: localhost
  tasks:
    - name: find the file
      copy:
        src: "{{ item }}"
        dest: "/home/ec2-user/bb"
        remote_src: yes
      with_fileglob:
        - "/home/ec2-user/aa/Data*"
      register: result
    - name: show the result file path
      debug:
        msg: "{{ item.path }}"
      loop: "{{ result.files }}"
    - name: extract file
      unarchive:
        src: "{{ item.path }}"
        dest: /home/ec2-user/cc/
      loop: "{{ result.files }}"

the error is:

fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'files'"}

1 Answer 1

1

You are registering the output of the copy module in the result variable. When the copy module runs in a loop, it returns a results key that contains each item of the loop.

The copy module does not have a files return value. The return values are listed here. What you want to use is the dest value. The dest value is where you copied the file using the copy module, that will become your src value for the unarchive module.

You can adjust your playbook to use those variables like this:


- name: find and archive
  hosts: localhost
  tasks:
    - name: find the file
      copy:
        src: "{{ item }}"
        dest: "/home/ec2-user/bb"
        remote_src: yes
      with_fileglob:
        - "/home/ec2-user/aa/Data*"
      register: result
    - name: show the result file path
      debug:
        msg: "{{ item.dest }}"
      loop: "{{ result.results }}"
    - name: extract file
      unarchive:
        src: "{{ item.dest }}"
        dest: /home/ec2-user/cc/
      loop: "{{ result.results }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.