Ansible module is list of available libraries can be used to perform various task on remote server with the help of playbook.
Default package of Ansible contents near about 1000+ various modules.
To get this list of such available module on your Ansible Tower run :
[root@siddhesh ~]# ansible-doc -l
This will show you list of available module and one liner description about module.
So starting from this series we are going to cover mostly used Ansible modules in playbook.
Ansible Copy Module :
Ansible Copy module can be used to copy file & folder from the local server (Ansible Tower) to remote server.
So lets try understand working of this module by creating playbook.
Scenario 1 : Copy File from local server to remote server.
[root@siddhesh ~]# cat copy.yml
- hosts: dbserver
tasks:
- name: copy local file to remote server
copy:
src: ~/siddhesh_test.txt
dest: /tmp
[root@siddhesh ~]#
Here :
- hosts: dbserver ==> Host Inventory Group On which this action needs to be performed.
- name: copy local file to remote server ==> Task Description
copy: ==> Load Copy Module src: ~/siddhesh_test.txt ==> Source file path of local server
dest: /tmp ==> Destination path of remote server.
Now lets run this playbook to see its output.
[root@siddhesh ~]# ansible-playbook copy.yml
PLAY [dbserver] *****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]
TASK [copy local file to remote server] *****************************************************************************************************************************
changed: [node1.tecgeek.info]
PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info : ok=2 changed=1 unreachable=0 failed=0
[root@siddhesh ~]#
Scenario 2 : Copy File from local server to remote server without overwriting. By default copy module overwrite file on remote server to avoid this you can add force: no
at the end of playbook file to avoid overwriting file at remote server side.
[root@siddhesh ~]# cat copy.yml
- hosts: dbserver
tasks:
- name: copy local file to remote server
copy:
src: ~/siddhesh_test.txt
dest: /tmp
force: no
[root@siddhesh ~]#
Now lets try to run same playbook again to see if it is really working.
[root@siddhesh ~]# ansible-playbook copy.yml
PLAY [dbserver] *****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]
TASK [copy local file to remote server] *****************************************************************************************************************************
ok: [node1.tecgeek.info]
PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info : ok=2 changed=0 unreachable=0 failed=0
[root@siddhesh ~]#
Here you can changed count shows 0(zero). So like this you can use multiple additional checksum in your playbook while copying file to remote server. To get a list of such supported argument you can run :
[root@siddhesh ~]# ansible-doc -s copy
Scenario 3 : Copy Folder from local server to remote server.
In this scenario we'll see how we can copy multiple files and folders to remote server.
[root@siddhesh ~]# cat copy.yml
- hosts: dbserver
tasks:
- name: copy local directory to remote server
copy:
src: /opt/ansibleroot
dest: /opt
[root@siddhesh ~]#
On local server I have following folder structure which I want to copy on remote server.
[root@siddhesh ~]# tree /opt/ansibleroot/
/opt/ansibleroot/
├── test1
├── test2
│ └── testsid
├── test3
│ └── testsiddd
└── test4
└── 100.txt
3 directories, 4 files
[root@siddhesh ~]#
So lets run this playbook to see what it copies now.
[root@siddhesh ~]# ansible-playbook copy.yml
PLAY [dbserver] *****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]
TASK [copy local directory to remote server] ************************************************************************************************************************
changed: [node1.tecgeek.info]
PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info : ok=2 changed=4 unreachable=0 failed=0
[root@siddhesh ~]#
Login to remote server and verify if you have got same directory structure.
[root@node1 ~]# tree /opt/ansibleroot/
/opt/ansibleroot/
├── test1
├── test2
│ └── testsid
├── test3
│ └── testsiddd
└── test4
└── 100.txt
3 directories, 4 files
[root@node1 ~]#
Comments