Software >> Automation >> Ansible >> Modules >> fetch >> Examples

 

fetch

online help: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fetch_module.html
offline help: ansible-doc fetch
 

playbook :


fetch1.yml
 
---
- name: Fetch hosts file from remote servers
  hosts: all
  become: True
  tasks:
     - name: Fetch the output file
       fetch:
         src: /etc/hosts
         dest: /home/ansible/

 

output :

The files fetched from the respective remote servers will be placed under a subdirectory named as the hostname of that server.  The directory hierarchy will be copied as well.

See next example for alternative way of saving at the destination.
 

playbook :

fetch2.yml

---
- name: Fetch hosts file remote servers
  hosts: all
  become: True
  tasks:
     - name: Fetch the output file
       fetch:
         src: /etc/hosts
         dest: /home/ansible/fetched/{{ansible_host}}_hosts
         flat: yes



output :




 

 

 

 

 

 

References

  1. ansible.builtin plugins and modules index
  2. ansible collections index
  3. ansible loops
  4. ansible conditionals
  5. ansible playbook error handling
  6. ansible playbook tests
  7. ansible playbook blocks
  8. ansible playbook roles
  9. ansible playbook files