Ansible Replace Module

Ticker

6/recent/ticker-posts

Ansible Replace Module

 Question : There is 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. The DevOps team is working to prepare an Ansible playbook to accomplish the same. Below you can find more details about the task.

Create a playbook.yml under /home/thor/ansible on jump host; an inventory is already place under /home/thor/ansible on Jump host itself.

We have a file /opt/dba/blog.txt on app server 1. Using Ansible replace module replace string xFusionCorp to Nautilus in that file.

We have a file /opt/dba/story.txt on app server 2. Using Ansiblereplace module replace string Nautilus to KodeKloud in that file.

We have a file /opt/dba/media.txt on app server 3. Using Ansible replace module replace string KodeKloud to xFusionCorp Industries in that file.


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1Go through the folder mentioned in task and verified the playbook     

thor@jump_host /$ cd /home/thor/ansible

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ ll

total 8

-rw-r--r-- 1 thor thor  36 Jul  4 08:02 ansible.cfg

-rw-r--r-- 1 thor thor 237 Jul  4 08:02 inventory

thor@jump_host ~/ansible$


2.  Create a playbook as per the task  ( refer Video below for clarity ).

thor@jump_host ~/ansible$ vi playbook.yml

thor@jump_host ~/ansible$ cat playbook.yml

- name: Ansible replace

  hosts: stapp01,stapp02,stapp03

  become: yes

  tasks:

    - name: blog.txt replacement

      replace:

        path: /opt/dba/blog.txt

        regexp: "xFusionCorp"

        replace: "Nautilus"

      when: inventory_hostname == "stapp01"

    - name: story.txt replacement

      replace:

        path: /opt/dba/story.txt

        regexp: "Nautilus"

        replace: "KodeKloud"

      when: inventory_hostname == "stapp02"

    - name: media.txt replacement

      replace:

        path: /opt/dba/media.txt

        regexp: "KodeKloud"

        replace: "xFusionCorp Industries"

      when: inventory_hostname == "stapp03"

thor@jump_host ~/ansible$


3.  Post file saved , run below command to execute the playbook

thor@jump_host ~/ansible$ ansible-playbook -i inventory playbook.yml

 PLAY [Ansible replace] ******************************************************************************************************

 TASK [Gathering Facts] ******************************************************************************************************

ok: [stapp01]

ok: [stapp02]

ok: [stapp03]

 TASK [blog.txt replacement] *************************************************************************************************

skipping: [stapp02]

skipping: [stapp03]

changed: [stapp01]

 TASK [story.txt replacement] ************************************************************************************************

skipping: [stapp01]

skipping: [stapp03]

changed: [stapp02]

 TASK [media.txt replacement] ************************************************************************************************

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 login and cat the file
    

thor@jump_host ~/ansible$ ssh -t tony@stapp01 "cat /opt/dba/blog.txt"

The authenticity of host 'stapp01 (172.16.238.10)' can't be established.

ECDSA key fingerprint is SHA256:qwM1jr6+T0i1O57EFVwP2zH82tn1D5YULj+tCHzCSos.

ECDSA key fingerprint is MD5:09:77:3b:f3:a1:9f:8b:51:19:84:7e:57:8b:0a:4d:1c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'stapp01' (ECDSA) to the list of known hosts.

tony@stapp01's password:

Welcome to Nautilus Industries !

Connection to stapp01 closed.

thor@jump_host ~/ansible$


5. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user