Please explain copy tasks in the playbook

Hello,
can someone please explain what the copy tasks is doing in this playbook?

hosts: web1
tasks:
- name: Find files
find:
paths: /opt/data
age: 2m
size: 1m
recurse: yes
register: file

- name: Copy files
  command: "cp {{ item.path }} /opt"
  with_items: "{{ file.files }}"

Hello @domingochavez16,

This playbook will copy all the files in path /opt/data with age: 2m and size: 1m to /opt

I had the same question but I need more detail. How did the writer of this script know to use the value file.files to use in the with_items parameter. Meaning. I dont see any item named files. I can see that the return value of find goes into the variable file but where does .files comes from? Also where does item.path come from. I dont see those variable names.

Thanks

I’m new here and unsure if you wanted the answer or not (unsure if you know now) hopefully this will help someone in the future

(1) How did the writer of this script know to use the value file.files to use in the with_items parameter.

You can view the values of a register by debugging it (see here:https://www.mydailytutorials.com/ansible-register-variables/). I havent seen the output here is something similar to what it should be (see ```
TASK [print find_result] ) (https://stackoverflow.com/questions/41565614/ansible-how-to-register-output-from-find-module-and-use-in-other) is similar

(2)Also where does item.path come from. I dont see those variable names.

file.files = item to look at
item.path == file.files.path - loop on this

the syntax is technically incorrect since “loop” should be used over with_items (see here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html)

hope this helps