KodeKloud Engineer Day 93: Using Ansible Conditionals.
Hey Everyone! I have taken up the kodekloud engineer daily challenge [100 days of Devops] and through this series, we shall slowly but…
KodeKloud Engineer Day 93: Using Ansible Conditionals.
Hey Everyone! I have taken up the kodekloud engineer daily challenge [100 days of Devops] and through this series, we shall slowly but steadily start solving the daily challenges.
Here’s the link to get started with the challenge: https://engineer.kodekloud.com/practice
Day #93 Task: Using Ansible Conditionals.
The Nautilus DevOps team had a discussion about, 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 that Ansible offers. The team wants to utilise Ansible’s conditionals to perform the following task:
An
inventoryfile is already placed under/home/thor/ansibledirectory onjump host, with all theStratos DC app serversincluded.
Create a playbook
/home/thor/ansible/playbook.ymland make sure to use Ansible'swhenconditionals statements to perform the below given tasks.
- Copy
blog.txtfile present under/usr/src/dbadirectory onjump hosttoApp Server 1under/opt/dbadirectory. Its user and group owner must be usertonyand its permissions must be0655.
- Copy
story.txtfile present under/usr/src/dbadirectory onjump hosttoApp Server 2under/opt/dbadirectory. Its user and group owner must be usersteveand its permissions must be0655.
- Copy
media.txtfile present under/usr/src/dbadirectory onjump hosttoApp Server 3under/opt/dbadirectory. Its user and group owner must be userbannerand its permissions must be0655.
NOTE:You can useansible_nodenamevariable from gathered facts withwhencondition. Additionally, please make sure you are running the play for all hosts i.e use- hosts: all.
Note:Validation will try to run the playbook using commandansible-playbook -i inventory playbook.yml, so please make sure the playbook works this way without passing any extra arguments.

Day #93 Task: Using Ansible Conditionals.
Step-1: Verify the existing inventory file.
Let’s just check the content of the existing inventory file.
thor@jump-host ~/ansible$ cat inventory
stapp01 ansible_host=stapp01 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=stapp02 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=stapp03 ansible_ssh_pass=BigGr33n ansible_user=banner
This means that the playbook that we’ll create will run on all the three app-servers.
Step-2: Create the playbook with the defined requirements.
Thewhen conditionals in Ansible are used to decide: “Should this task run on this host or not?”. You can think of it like an if statement in programming. So, for this problem statement, depending on what the *ansible_nodename*variable points to, the different conditions are executed.
As seen in the problem statement, there are 3 tasks to include in the playbook. Using the *whenansible conditional and the `ansible_nodename`*variable, we must do the following:
- When the
*ansible_nodenameis stapp01, copy `blog.txt* file from path/usr/src/dba`in jump host to path*/opt/dba*in App Server 1. - When the
*ansible_nodenameis stapp02, copy `story.txt* file from path/usr/src/dba`in jump host to path*/opt/dba*in App Server 2. - When the
*ansible_nodenameis stapp03, copy `media.txt* file from path/usr/src/dba`in jump host to path*/opt/dba*in App Server 3.
For all the 3 task, the ownership of the file and the group of the file, must be the respective ansible host name and the permission must be set to ‘*0655*’.
thor@jump-host ~/ansible$ cat playbook.yml
# Content of the playbook.yml file.
---
- hosts: all
become: yes
tasks:
- name: Copy blog.txt file from jump host to App Server 1
copy:
src: /usr/src/dba/blog.txt
dest: /opt/dba/blog.txt
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0655'
when: ansible_nodename == "stapp01"
- name: Copy story.txt file from jump host to App Server 2
copy:
src: /usr/src/dba/story.txt
dest: /opt/dba/story.txt
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0655'
when: ansible_nodename == "stapp02"
- name: Copy media.txt file from jump host to App Server 3
copy:
src: /usr/src/dba/media.txt
dest: /opt/dba/media.txt
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0655'
when: ansible_nodename == "stapp03"
Step-3: Validate the execution of the playbook against the inventory file.
Check in all the app servers, before executing the playbook file, if any such file exists.
# Verify if the respective files exist in the respective App server before executing the playbook.
# In App Server 01:
thor@jump-host ~$ ssh tony@stapp01
The authenticity of host 'stapp01 (10.244.49.4)' can't be established.
ED25519 key fingerprint is SHA256:lspNaglQUfasQmk6xfii7Xxdgz05wor0bijKdCYDUuk.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp01' (ED25519) to the list of known hosts.
tony@stapp01's password:
[tony@stapp01 ~]$ ls -la /opt/dba
total 12
drwxr-xr-x 2 root root 4096 May 25 11:22 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
# In App Server 02:
thor@jump-host ~$ ssh steve@stapp02
The authenticity of host 'stapp02 (10.244.195.116)' can't be established.
ED25519 key fingerprint is SHA256:ZvVp/yIFokW4JD6woDuKX5GKMu8Q/nZeo9VY9GdHDnM.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp02' (ED25519) to the list of known hosts.
steve@stapp02's password:
[steve@stapp02 ~]$ ls -la /opt/dba
total 12
drwxr-xr-x 2 root root 4096 May 25 11:22 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
# In App Server 03:
thor@jump-host ~$ ssh banner@stapp03
The authenticity of host 'stapp03 (10.244.240.73)' can't be established.
ED25519 key fingerprint is SHA256:M7rxaatmWVTP88Z3/PuMM9XEqfjOhpE0f3ArnUZqSn0.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp03' (ED25519) to the list of known hosts.
banner@stapp03's password:
[banner@stapp03 ~]$ ls -la /opt/dba
total 12
drwxr-xr-x 2 root root 4096 May 25 11:22 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
# Verify the content of the files to be copied from the jumphost:
thor@jump-host ~/ansible$ cat /usr/src/dba/blog.txt
Welcome to xFusionCorp Industries !
thor@jump-host ~/ansible$ cat /usr/src/dba/media.txt
Welcome to KodeKloud !
thor@jump-host ~/ansible$ cat /usr/src/dba/story.txt
Welcome to Nautilus Group !
# Executing the playbook and verifying if the files got copied successfully and verifying the ownership and the permission of the copied file.
thor@jump-host ~/ansible$ ansible-playbook -i inventory playbook.yml
PLAY [all] ******************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [stapp02]
ok: [stapp03]
ok: [stapp01]
TASK [Copy blog.txt file from jump host to App Server 1] ********************************
skipping: [stapp02]
skipping: [stapp03]
changed: [stapp01]
TASK [Copy story.txt file from jump host to App Server 2] *******************************
skipping: [stapp01]
skipping: [stapp03]
changed: [stapp02]
TASK [Copy media.txt file from jump host to App Server 3] *******************************
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
# Verify if the files got copied successfully and verify the ownership and the permission of the copied file.
# In App server 01:
[tony@stapp01 ~]$ ls -la /opt/dba
total 16
drwxr-xr-x 2 root root 4096 May 25 11:37 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
-rw-r-xr-x 1 tony tony 35 May 25 11:37 blog.txt
[tony@stapp01 ~]$ cat /opt/dba/blog.txt
Welcome to xFusionCorp Industries !
# In App server 02:
[steve@stapp02 ~]$ ls -la /opt/dba
total 16
drwxr-xr-x 2 root root 4096 May 25 11:38 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
-rw-r-xr-x 1 steve steve 27 May 25 11:37 story.txt
[steve@stapp02 ~]$ cat /opt/dba/story.txt
Welcome to Nautilus Group !
# In App Server 03:
[banner@stapp03 ~]$ ls -la /opt/dba
total 16
drwxr-xr-x 2 root root 4096 May 25 11:38 .
drwxr-xr-x 1 root root 4096 May 25 11:22 ..
-rw-r-xr-x 1 banner banner 22 May 25 11:38 media.txt
[banner@stapp03 ~]$ cat /opt/dba/media.txt
Welcome to KodeKloud !
That’s it for Day #93.
With this, we’ve completed all the Ansible related challenges! Congratulations for making it so far! 🎉🎉🎉 Let’s hop on to the next challenge! See you there! Toodaloo!
메타데이터
- post_id
- 8ef8a0ff2a72
- slug
- kodekloud-engineer-day-93-using-ansible-conditionals-8ef8a0ff2a72
- url
- https://medium.com/@janemils/kodekloud-engineer-day-93-using-ansible-conditionals-8ef8a0ff2a72
- canonical_url
- https://medium.com/@janemils/kodekloud-engineer-day-93-using-ansible-conditionals-8ef8a0ff2a72
- author_url
- https://medium.com/@janemils
- status
- ok
- fetched_at
- 2026-06-09 15:37:30