There is some data on all app servers in Stratos DC. The Nautilus development team shared some requirement with the DevOps team to alter some of the data as per recent changes they made. The DevOps team is working to prepare an Ansible playbook to accomplish the same. Below you can find more details about the task.
Write a playbook.yml under /home/thor/ansible on jump host, an inventory is already present under /home/thor/ansible directory on Jump host itself. Perform below given tasks using this playbook:
We have a file /opt/devops/blog.txt on app server 1. Using Ansible replace module replace string xFusionCorp to Nautilus in that file.
We have a file /opt/devops/story.txt on app server 2. Using Ansible replace module replace the string Nautilus to KodeKloud in that file.
We have a file /opt/devops/media.txt on app server 3. Using Ansible replace module replace string KodeKloud to xFusionCorp Industries in that file.
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.
-
Navigate to the Ansible Directory
cd /home/thor/ansible
-
Create the Ansible Playbook File
vi playbook.yml
-
Add the Playbook Content
Insert the following YAML configuration into the
playbook.yml
file:
---
- hosts: all
become: yes
tasks:
- name: Replace string in /opt/devops/blog.txt on stapp01
replace:
path: /opt/devops/blog.txt
regexp: 'xFusionCorp'
replace: 'Nautilus'
when: inventory_hostname == 'stapp01'
- name: Replace string in /opt/devops/story.txt on stapp02
replace:
path: /opt/devops/story.txt
regexp: 'Nautilus'
replace: 'KodeKloud'
when: inventory_hostname == 'stapp02'
- name: Replace string in /opt/devops/media.txt on stapp03
replace:
path: /opt/devops/media.txt
regexp: 'KodeKloud'
replace: 'xFusionCorp Industries'
when: inventory_hostname == 'stapp03'
-
Run the Playbook
Execute the following command to run the playbook using the inventory file:
ansible-playbook -i inventory playbook.yml
Upon running the playbook, you should see output similar to the following:
PLAY [all] ******************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [stapp03]
ok: [stapp01]
ok: [stapp02]
TASK [Replace string in /opt/devops/blog.txt on stapp01] ********************************
skipping: [stapp02]
skipping: [stapp03]
changed: [stapp01]
TASK [Replace string in /opt/devops/story.txt on stapp02] *******************************
skipping: [stapp01]
skipping: [stapp03]
changed: [stapp02]
TASK [Replace string in /opt/devops/media.txt on 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