Gonçalo Sousa:
Hello,
I am not understanding how this playbook was constructed (Ansible Advanced Course - File content lab)
---
- 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 }}"
We are using register to register the result as a variable I am not understanding how and why item.path and file.files is being called
Andrei Andriushin:
Hi Gonçalo,
I’m glad you’re joined community!
- Take a look at find module description: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html - it return lists with files metadata dictionaries, which is saved in
file
variable
- You can use
debug
module to print variable contents - https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html
- Another way is to use
-v
argument and show task output in console. (Use -vvv
for human-readable json format)
- Next task uses
with_items
option which runs it in loop, based on items from file
list
-
path
is one of file’s metadata, which is saved as a dictionary items inside list elements by previous task.