Using Ansible Conditionals

Task :

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.

Create a playbook /home/thor/ansible/playbook.yml and make sure to use Ansible’s when conditionals statements to perform the following tasks.

Copy blog.txt file present under /usr/src/security directory on jump host to App Server 1 under /opt/security directory. Its user and group owner must be user tony and its permissions must be 0777 .

Copy story.txt file present under /usr/src/security directory on jump host to App Server 2 under /opt/security directory. Its user and group owner must be user steve and its permissions must be 0777 .

Copy media.txt file present under /usr/src/security directory on jump host to App Server 3 under /opt/security directory. Its user and group owner must be user banner and its permissions must be 0777 .

 

NOTE: You can use ansible_nodename variable from gathered facts with when condition. Additionally, please make sure you are running the play for all hosts i.e use – hosts: all.

Note: Validation will try to run playbook using command ansible-playbook -i inventory playbook.yml, so please make sure playbook works this way, without passing any extra arguments.

Solution :

thor@jump_host /$ vi /home/thor/ansible/playbook.yml
- name: Copy files to Appservers
  hosts: all
  become: yes
  tasks:
    - name: Copy blog.txt to stapp01
      ansible.builtin.copy:
        src: /usr/src/security/blog.txt
        dest: /opt/security/
        owner: tony
        group: tony
        mode: "0777"
        when: inventory_hostname == "stapp01"
    - name: Copy story.txt to stapp02
      ansible.builtin.copy:
        src: /usr/src/security/story.txt
        dest: /opt/security/
        owner: steve
        group: steve
        mode: "0777"
        when: inventory_hostname == "stapp02"
    - name: Copy media.txt to stapp03
      ansible.builtin.copy:
        src: /usr/src/security/media.txt
        dest: /opt/security/
        owner: banner
        group: banner
        mode: "0777"
        when: inventory_hostname == "stapp03"
thor@jump_host /$ cd /home/thor/ansible/
thor@jump_host ~/ansible$ ls
ansible.cfg inventory playbook.yml
thor@jump_host ~/ansible$ cat inventory 
stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner
thor@jump_host ~/ansible$ ansible-playbook -i inventory playbook.yml
PLAY [Copy files to Appservers] ********************************************************

TASK [Gathering Facts] *****************************************************************
ok: [stapp03]
ok: [stapp01]
ok: [stapp02]

TASK [Copy blog.txt to stapp01] ********************************************************
skipping: [stapp02]
skipping: [stapp03]
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$

Leave a Reply

Your email address will not be published. Required fields are marked *