Third Ansible playbook introducing usage of copy module, tags, and how to disable gather facts step.

Till now we have looked at ansible playbook to get the ping response, server uptime, date and its syntax.

In this post, we will see how to copy a file, and check for copied file on the server.

Also we will check how to capture the task output and use of tags in ansible playbook.

1. Ansible playbook to copy a file and verify the copied file.

$ cat playbooks/thirdplay.yml 
---
 - name: "Copying test.html file"
   hosts: all
   gather_facts: no
   tasks:
   - action: copy src="../files/test.html" dest="~/"


 - name: "Verify Copied File"
   hosts: all
   tags: verifylist
   gather_facts: no
   tasks: 
   - action: "shell ls -l"
     register: hello
   - debug: msg="{{ hello.stdout }}" 

Explanation
Now from here we will check the new options not covered in last ansible playbook.
a. “gather_facts: no” will make ansible to avoid gathering the facts about the host. By default, it gathers some details about each host. It helps to speed up the task. However it should not be avoided.
b. “copy” module is used to copy the “src” file into “dest” path.


2. Output

$ ansible-playbook playbooks/thirdplay.yml 

PLAY [Copying test.html file] ******************************************************************************************************************************************************************

TASK [copy] ************************************************************************************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

PLAY [Verify Copied File] **********************************************************************************************************************************************************************

TASK [shell] ***********************************************************************************************************************************************************************************
changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "total 8\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html\n-rw-r--r-- 1 root     root     25 Oct  5 06:16 test.txt"
}
ok: [r168-host1.ngelinux.com] => {
    "msg": "total 4\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html"
}

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   
r168-host1.ngelinux.com     : ok=3    changed=1    unreachable=0    failed=0   

$ 

3. Use of Tags
Suppose you just want to verify if the file exists or not. And dont want to run copy task to save some time.

In this case we can add the tags to the tasks and can run any specific tag task.

Lets see this in example below.

$ ansible-playbook playbooks/thirdplay.yml  -t verifylist

PLAY [Copying test.html file] ******************************************************************************************************************************************************************

PLAY [Verify Copied File] **********************************************************************************************************************************************************************

TASK [shell] ***********************************************************************************************************************************************************************************
changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "total 8\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html\n-rw-r--r-- 1 root     root     25 Oct  5 06:16 test.txt"
}
ok: [r168-host1.ngelinux.com] => {
    "msg": "total 4\n-rw-r--r-- 1 c5014038 aplstaff 20 Dec  3 05:12 test.html"
}

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=2    changed=1    unreachable=0    failed=0   
r168-host1.ngelinux.com     : ok=2    changed=1    unreachable=0    failed=0   


### You can see the current date below is 10th december, however the copied file exists since 3rd december.
$ date
Mon Dec 10 03:04:49 PST 2018
$ 

Leave a Reply

Your email address will not be published.