As a part of IT management department, folks deal with lots of scripts
and command line tools. I’m sure you face day to day situation in which you have copy files or scripts to thousands of remote hosts.
IT department folks don’t just login to each thousands of hosts
and run script manually. And that’s why Ansible comes into picture. Ansible is a very powerful IT management solution out there for us at free of cost.
In this tutorial we will go over steps to copy files to remote host using Ansible.
What we are going to do?
Copy file crunchify.txt
file from location /opt/ashah/
to remote host 192.66.129.83
‘s destination directory /opt/ashah/
.
Step-1
Install Ansible on Linux Ubuntu or macOS. Follow tutorials to go over detailed steps.
Step-2
Let’s understand some basics and file location for this tutorial:
- Config file location: /etc/ansible/ansible.cfg
- Script file: /opt/ashah/crunchify.yml
- hosts file: /opt/ashah/hosts
- file to copy: /opt/ashah/crunchify.txt (test file)
Step-3
crunchify.yml
file content.
root@localhost:/opt/ashah# cat crunchify.yml --- - hosts: crunchify-group tasks: - name: copy file to remote host copy: src: /opt/ashah/crunchify.txt dest: /opt/ashah
Step-4
hosts
file content. Make sure to change IP address to your remote host.
root@localhost:/opt/ashah# cat hosts #crunchify-group as a sample [crunchify-group] 192.66.129.83
Step-5
Create file crunchify.txt
with below content.
root@localhost:/opt/ashah# cat crunchify.txt This is sample example copying file to remote location using Ansible. More ansible tutorials at https://crunchify.com/tag/ansible/
Step-6
Run this command to copy file to remote host:
Command
: ansible-playbook -b -vvv -u root crunchify.yml -kkkk –extra-vars “crunchify-group” -i hosts
let’s understand all parameters:
- -b : run operations with become (same as —become).
- -vvv : verbose mode (-vvv for more, -vvvv to enable connection debugging).
- -u : connect as this user (default=None).
- -kkkk : ask for connection password.
- –extra-vars : set additional variables as key=value or YAML/JSON, if filename prepend with @.
- -i : specify inventory host path or comma separated host list.
root@localhost:/opt/ashah# ansible-playbook -b -vvv -u root crunchify.yml -kkkk --extra-vars "crunchify-group" -i hosts Using /etc/ansible/ansible.cfg as config file SSH password: /opt/ashah/hosts did not meet host_list requirements, check plugin documentation if this is unexpected /opt/ashah/hosts did not meet script requirements, check plugin documentation if this is unexpected Parsed /opt/ashah/hosts inventory source with ini plugin PLAYBOOK: crunchify.yml ************************************************************************************************************************* 1 plays in crunchify.yml PLAY [crunchify-group] ********************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************ <192.66.129.83> ESTABLISH SSH CONNECTION FOR USER: root <192.66.129.83> SSH: EXEC sshpass -d9 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/5fca43321f 192.66.129.83 '/bin/sh -c '"'"'echo ~root && sleep 0'"'"'' <192.66.129.83> (0, '/root\n', '') <192.66.129.83> ESTABLISH SSH CONNECTION FOR USER: root <192.66.129.83> SSH: EXEC sshpass -d9 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/5fca43321f 192.66.129.83 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" && echo ansible-tmp-1550966284.57-6253496643791="` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" ) && sleep 0'"'"'' <192.66.129.83> (0, 'ansible-tmp-1550966284.57-6253496643791=/root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791\n', '') Using module file /usr/lib/python2.7/dist-packages/ansible/modules/system/setup.py <192.66.129.83> PUT /root/.ansible/tmp/ansible-local-32435HXzj7o/tmpjFxUXp TO /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/AnsiballZ_setup.py <192.66.129.83> SSH: EXEC sshpass -d9 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/5fca43321f '[192.66.129.83]' <192.66.129.83> (0, 'sftp> put /root/.ansible/tmp/ansible-local-32435HXzj7o/tmpjFxUXp /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/AnsiballZ_setup.py\n', '') <192.66.129.83> ESTABLISH SSH CONNECTION FOR USER: root <192.66.129.83> ESTABLISH SSH CONNECTION FOR USER: root <192.66.129.83> SSH: EXEC sshpass -d9 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/5fca43321f 192.66.129.83 '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/ > /dev/null 2>&1 && sleep 0'"'"'' <192.66.129.83> (0, '', '') ok: [192.66.129.83] TASK [deploy topology] ******************************************************************************************************************************************** ok: [192.66.129.83] => { "changed": false, "checksum": "289b0c36372210c92bf028f2a19d57333af4d63c", "dest": "/opt/ashah/crunchify.txt", "diff": { "after": { "path": "/opt/ashah/crunchify.txt" }, "before": { "path": "/opt/ashah/crunchify.txt" } }, "gid": 0, "group": "root", "invocation": { "module_args": { "_diff_peek": null, "_original_basename": "crunchify.txt", "access_time": null, "access_time_format": "%Y%m%d%H%M.%S", "attributes": null, "backup": null, "content": null, "delimiter": null, "dest": "/opt/ashah", "directory_mode": null, "follow": true, "force": false, "group": null, "mode": null, "modification_time": null, "modification_time_format": "%Y%m%d%H%M.%S", "owner": null, "path": "/opt/ashah/crunchify.txt", "recurse": false, "regexp": null, "remote_src": null, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "state": "file", "unsafe_writes": null } }, "mode": "0644", "owner": "root", "path": "/opt/ashah/crunchify.txt", "size": 21, "state": "file", "uid": 0 } PLAY RECAP ******************************************************************************************************************************************************** 192.66.129.83 : ok=2 changed=0 unreachable=0 failed=0
That’s it. You have successfully copied file.
How to verify if file is copied over to new remote host?
Just login to remote host and check location /opt/ashah
and you will see file crunchify.txt
🙂
How to copy a directory content to another directory?
Just change src
and desc
value of crunchify.yml
file. Here is an updated crunchify.yml file.
root@localhost:/opt/ashah# cat crunchify.yml --- - hosts: crunchify-group tasks: - name: copy all files from folder to remote host copy: src: /opt/ashah/ dest: /opt/ashah/
All files under /opt/ashah/
folder will be copied to remote hosts now.
Let me know if you have any question coping file to remote location using Ansible.