Using Ansible Conditionals Kodekloud

 

Question:  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 inventory file is already placed under /home/thor/ansible directory 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 below given tasks.

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

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

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

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 the playbook using command ansible-playbook -i inventory playbook.yml, so please make sure the playbook works this way without passing any extra arguments.


Solution:

Create a playbook on ansible folder by following question requirements

cd 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/finance/blog.txt
dest: /opt/finance/
owner: tony
group: tony
mode: "0644"
when: inventory_hostname == "stapp01"
- name: Copy story.txt to stapp02
ansible.builtin.copy:
src: /usr/src/finance/story.txt
dest: /opt/finance/
owner: steve
group: steve
mode: "0644"
when: inventory_hostname == "stapp02"
- name: Copy media.txt to stapp03
ansible.builtin.copy:
src: /usr/src/finance/media.txt
dest: /opt/finance/
owner: banner
group: banner
mode: "0644"
when: inventory_hostname == "stapp03"

Then apply the ansible playbook

ansible-playbook -i inventory playbook.yml



Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment