While the copy module helps to copy a file from the controller node to managed nodes, the fetch module helps to retrieve files from a managed node to controller node.
In the below example we will write a playbook to fetch the secure log file from the managed node to the controller node. Pay close attention to the flat parameter used by the fetch module.
Let us ensure that there is NO folder named secure-backups in the present working directory
[root@centos9vm ~]# ls -l | grep secure-backups
Create a playbook that will copy the /var/log/secure file from the managed node to the control node and preserver the folder structure. Pay attention to the flat parameter.
[root@centos9vm ~]# cat secure_log_backups.yml
====== ====
– – –
– name: Use the fetch module to retrieve secure log file
hosts: 192.168.48.129
remote_user: root
tasks:
– name: Fetch secure log files
ansible.builtin.fetch:
src: /var/log/secure
dest: secure-backups
flat: no
===== === ==
[root@centos9vm ~]# ansible-navigator run -m stdout secure_log_backups.yml
===== ===
PLAY [Use the fetch module to retrieve secure log file] ************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]
TASK [Fetch secure log files] **************************************************
changed: [192.168.48.129]
PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
==== ====
Let us see if the secure-backups folder for created automatically.
[root@centos9vm ~]# ls -ld secure-backups
===== === ==
drwxr-xr-x. 3 root root 28 Mar 23 13:31 secure-backups
===== === ==
Now let us see if the secure file got copied to the controller node, preserving the folder structure
[root@centos9vm ~]# ls -l secure-backups/192.168.48.129/var/log/secure
===== === ==
-rw——-. 1 root root 11791 Mar 23 13:31 secure-backups/192.168.48.129/var/log/secure
===== === ==
As you can see above, the secure-backups folder got created, a folder named with managed node’s name and also sub-folders that matches the place where the file is stored in the managed node.
Let us delete the secure-backups folder and also tweak the playbook a bit and change the value of flat parameter.
[root@centos9vm ~]# rm -rf secure-backups
[root@centos9vm ~]# ls -ld secure-backups
ls: cannot access ‘secure-backups’: No such file or directory
[root@centos9vm ~]# cat secure_log_backups.yml
====== ==
– – –
– name: Use the fetch module to retrieve secure log file
hosts: 192.168.48.129
remote_user: root
tasks:
– name: Fetch secure log files
ansible.builtin.fetch:
src: /var/log/secure
dest: secure-backups
flat: yes
==== ===
Now let us run the playbook
[root@centos9vm ~]# ansible-navigator run -m stdout secure_log_backups.yml
====== =
PLAY [Use the fetch module to retrieve secure log file] ************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]
TASK [Fetch secure log files] **************************************************
changed: [192.168.48.129]
PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
====== ===
Now if we check we will find that the log file secure got written to a file named secure-backups.
[root@centos9vm ~]# ls -ld secure-backups
-rw——-. 1 root root 12158 Mar 23 13:39 secure-backups
[root@centos9vm ~]# wc -l secure-backups
104 secure-backups