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: ""
     - name: Upload directories
    	  mode: push
    	  dest: /home/user/
    	  src: /home/user/ansible
  	delegate_to: "localhost"

- name: Display directory
     - name: initialize command
    	  cmd_str: "ls /home/user/ansible/"

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

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


$ ansible-playbook upload_files.yml

PLAY [Upload directories to localhost] ************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4 on host 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 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: []

TASK [Upload directories] *************************************************************************************************************************************************
changed: [ -> localhost]

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

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

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

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

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

PLAY RECAP ******************************************************************************************************************************************************************          	: 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