How to download a directory from a remote machine in ansible?

How to download a directory from a remote machine in ansible?

In ansible, we are going to see how to download files or directory from remote machine with an example ansible playbook.

synchronize is a wrapper around rsync to make common tasks quick and easy to use in playbooks.

mode is used to provide the direction of the synchronization.

Two modes possibe, one is push and another one is pull.

In push mode the localhost or delegate is the source, in pull mode the remote host is the source.

Lets see sample example ansible playbook to download files from remote machine.

---
- name: Download directories to localhost
  hosts: "121.68.1.51"
  tasks:
     - name: Download directories
  	synchronize:
    	  mode: pull
    	  src: /home/user/test
    	  dest: /home/user/ansible

- name: Display directory
  hosts: localhost
  tasks:
     - name: initialize command
  	set_fact:
    	  cmd_str: "ls /home/user/ansible/test"

      - name: "Executing {{ cmd_str }} in localhost machine"
  	command: "{{ cmd_str }}"
  	register: out

      - name: Print the msg
  	debug:
    	  msg: "{{ out.stdout }}"


Output:

$ ansible-playbook download_files.yml

PLAY [Download directories to localhost] ************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4 on host 121.68.1.51 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [121.68.1.51]

TASK [Download directories] *************************************************************************************************************************************************
changed: [121.68.1.51]

PLAY [Display directory] ****************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

TASK [initialize command] ***************************************************************************************************************************************************
ok: [localhost]

TASK [Executing ls /home/user/ansible/test in localhost machine] ***********************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "testing1.txt\ntesting2.txt\ntesting.txt"
}

PLAY RECAP ******************************************************************************************************************************************************************
121.68.1.51          	: ok=2	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Downloaded the files in test directory from remote machines to local machine.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^