How to upload a directory to remote machines in ansible?

How to upload a directory to remote machines in ansible?

In ansible, we are going to see how to upload files or directory to 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 upload files to remote machine from local machine.

---
- name: Upload directories to remote machine
  hosts: "121.68.1.51"
  tasks:
     - name: Upload directories
  	synchronize:
    	  mode: push
    	  dest: /home/user/
    	  src: /home/user/ansible
  	delegate_to: "localhost"

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

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

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

Output:

$ ansible-playbook upload_files.yml

PLAY [Upload 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 [Upload directories] *************************************************************************************************************************************************
changed: [121.68.1.51 -> localhost]

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

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

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

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

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [121.68.1.51] => {
	"msg": "ansible-errors.json\ndownload_files.yml\ntest.yml\nupload_files.yml"
}

PLAY RECAP ******************************************************************************************************************************************************************
121.68.1.51          	: ok=6	changed=2	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0  

Uploaded the files in ansible directory from local machine to remote machines.




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
^