{"id":1221,"date":"2024-03-23T07:55:03","date_gmt":"2024-03-23T07:55:03","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1221"},"modified":"2024-03-23T08:20:52","modified_gmt":"2024-03-23T08:20:52","slug":"ansible-the-fetch-module-to-retrieve-files","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1221","title":{"rendered":"Ansible &#8211; The fetch module to retrieve files"},"content":{"rendered":"<p>While the\u00a0<strong>copy<\/strong> 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.<\/p>\n<p>In the below example we will write a playbook to fetch the\u00a0<em><strong>secure<\/strong><\/em> log file from the managed node to the controller node. Pay close attention to the\u00a0<strong>flat<\/strong> parameter used by the\u00a0<em>fetch<\/em> module.<\/p>\n<p>Let us ensure that there is NO folder named\u00a0<strong>secure-backups<\/strong> in the present working directory<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ls -l | grep secure-backups<\/p>\n<p>Create a playbook that will copy the\u00a0<em>\/var\/log\/secure<\/em> file from the managed node to the control node and\u00a0<em>preserver<\/em> the folder structure. Pay attention to the <em>flat<\/em> parameter.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat secure_log_backups.yml<\/p>\n<p>====== ====<br \/>\n&#8211; &#8211; &#8211;<br \/>\n&#8211; name: Use the fetch module to retrieve secure log file<br \/>\n&nbsp; &nbsp; hosts: 192.168.48.129<br \/>\n&nbsp; &nbsp; remote_user: root<br \/>\n&nbsp; &nbsp; tasks:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Fetch secure log files<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.fetch:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: \/var\/log\/secure<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dest: secure-backups<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flat: no<\/p>\n<p>===== === ==<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout secure_log_backups.yml<\/p>\n<p>===== ===<\/p>\n<p><em>PLAY [Use the fetch module to retrieve secure log file] ************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Fetch secure log files] **************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n==== ====<\/p>\n<p>Let us see if the\u00a0<strong>secure-backups<\/strong> folder for created automatically.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ls -ld secure-backups<br \/>\n===== === ==<br \/>\ndrwxr-xr-x. 3 root root 28 Mar 23 13:31 secure-backups<br \/>\n===== === ==<br \/>\nNow let us see if the\u00a0<em>secure<\/em> file got copied to the controller node, preserving the folder structure<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ls -l secure-backups\/192.168.48.129\/var\/log\/secure<br \/>\n===== === ==<br \/>\n-rw&#8212;&#8212;-. 1 root root 11791 Mar 23 13:31 secure-backups\/192.168.48.129\/var\/log\/secure<br \/>\n===== === ==<\/p>\n<p>As you can see above, the\u00a0<em>secure-backups<\/em> folder got created, a folder named with managed node&#8217;s name and also sub-folders that matches the place where the file is stored in the managed node.<\/p>\n<p>Let us delete the <em>secure-backups<\/em> folder and also tweak the playbook a bit and change the value of\u00a0<em><strong>flat<\/strong><\/em> parameter.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> rm -rf secure-backups<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ls -ld secure-backups<br \/>\nls: cannot access &#8216;secure-backups&#8217;: No such file or directory<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat secure_log_backups.yml<\/p>\n<p>====== ==<br \/>\n&#8211; &#8211; &#8211;<br \/>\n&#8211; name: Use the fetch module to retrieve secure log file<br \/>\n&nbsp; &nbsp; hosts: 192.168.48.129<br \/>\n&nbsp; &nbsp; remote_user: root<br \/>\n&nbsp; &nbsp; tasks:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Fetch secure log files<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.fetch:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: \/var\/log\/secure<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dest: secure-backups<br \/>\n<span style=\"color: #ff0000;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flat: yes<\/span><br \/>\n==== ===<\/p>\n<p>Now let us run the playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout secure_log_backups.yml<\/p>\n<p>====== =<\/p>\n<p><em>PLAY [Use the fetch module to retrieve secure log file] ************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Fetch secure log files] **************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n====== ===<\/p>\n<p>Now if we check we will find that the log file\u00a0<em>secure <\/em>got written to a\u00a0<strong>file<\/strong> named\u00a0<strong>secure-backups<\/strong>.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ls -ld secure-backups<br \/>\n-rw&#8212;&#8212;-. 1 root root 12158 Mar 23 13:39 secure-backups<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> wc -l secure-backups<br \/>\n104 secure-backups<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>While the\u00a0copy module helps to copy a file from the controller node to managed nodes, the fetch module helps to retrieve files from a managed <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1221\" title=\"Ansible &#8211; The fetch module to retrieve files\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1221","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1221"}],"version-history":[{"count":11,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":1232,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1221\/revisions\/1232"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}