Question : The Nautilus DevOps team discussed how they can train different team members to use Ansible for different automation tasks. There are numerous ways to perform a particular task using Ansible, but we want to utilize each aspect Ansible offers. to the team wants to utilise Ansible's conditionals to perform the following task:
An inventory file is already placed under /home/thor/ansible on jump host, with all the Stratos DC app servers included.
1. At first ansible inventory file is working properly and doesn't have any file exist from jump server to all the app server's
thor@jump_host
/$ cd /home/thor/ansible/ thor@jump_host
~/ansible$ ll total 8 -rw-r--r-- 1
thor thor 36 Jun 21 07:24 ansible.cfg -rw-r--r-- 1
thor thor 237 Jun 21 07:24 inventory thor@jump_host
~/ansible$ thor@jump_host
~/ansible$ ansible all -a "ls -ltr /opt/dba" -i inventory stapp02 | FAILED
| rc=2 >> ls: cannot
access /opt/dba: No such file or directorynon-zero return code stapp01 | FAILED
| rc=2 >> ls: cannot
access /opt/dba: No such file or directorynon-zero return code stapp03 | FAILED
| rc=2 >> ls: cannot
access /opt/dba: No such file or directorynon-zero return code thor@jump_host
~/ansible$ |
thor@jump_host ~/ansible$ vi playbook.yml - name: Copy text files to
Appservers hosts:
all become:
yes tasks: - name: Copy blog.txt to stapp01 ansible.builtin.copy: src: /usr/src/dba/blog.txt dest: /opt/dba/ owner: tony group: tony mode: "0755" when: inventory_hostname ==
"stapp01" - name: Copy story.txt to stapp02 ansible.builtin.copy: src: /usr/src/dba/story.txt dest: /opt/dba/ owner: steve group: steve mode: "0755" when: inventory_hostname ==
"stapp02" - name: Copy media.txt to stapp03 ansible.builtin.copy: src: /usr/src/dba/media.txt dest: /opt/dba/ owner: banner group: banner mode: "0755" when: inventory_hostname ==
"stapp03" |
3. Post file saved , run below command to execute the playbook
thor@jump_host ~/ansible$ ansible-playbook -i inventory playbook.yml
PLAY [Copy text
files to Appservers] **********************************************************************
TASK [Gathering
Facts]
************************************************************************************ ok: [stapp02] ok: [stapp03] ok: [stapp01]
TASK [Copy
blog.txt to stapp01] *************************************************************************** skipping:
[stapp03] skipping:
[stapp02] changed:
[stapp01]
TASK [Copy
story.txt to stapp02]
************************************************************************** skipping:
[stapp01] skipping:
[stapp03] changed: [stapp02]
TASK [Copy
media.txt to stapp03]
************************************************************************** skipping:
[stapp01] skipping:
[stapp02] changed:
[stapp03]
PLAY RECAP
************************************************************************************************ stapp01 : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 stapp02 : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 stapp03 : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
thor@jump_host
~/ansible$ |
4. Validate the task by below command
thor@jump_host
~/ansible$ ansible all -a "ls -ltr /opt/dba" -i inventory stapp03 |
CHANGED | rc=0 >> -rwxr-xr-x 1
banner banner 22 Jun 21 07:43 /opt/dba stapp02 |
CHANGED | rc=0 >> total 4 -rwxr-xr-x 1
steve steve 27 Jun 21 07:43 story.txt stapp01 |
CHANGED | rc=0 >> total 4 -rwxr-xr-x 1
tony tony 35 Jun 21 07:43 blog.txt thor@jump_host
~/ansible$ |
Happy Learning!!!!
0 Comments