Ansible Lineinfile Module

Ticker

6/recent/ticker-posts

Ansible Lineinfile Module

 Question The Nautilus DevOps team want to install and set up a simple httpd web server on all app servers in Stratos DC. They also want to deploy a sample web page using Ansible only. Therefore, prepare the required playbook to complete this task. Find more details about the task below.

We already have an inventory file under /home/thor/ansible on jump host. Create a playbook.yml under /home/thor/ansible on jump host itself.

Using the playbook install httpd web server on all app servers, and make sure its service is up and running.

Create a file /var/www/html/index.html with content:

This is a Nautilus sample file, created using Ansible!

Using lineinfile Ansible module add some more content in /var/www/html/index.html file. Below is the content:

Welcome to xFusionCorp Industries!

Also make sure this new line is added at the top of the file.

The /var/www/html/index.html file's user and group owner should be apache on all app servers.

The /var/www/html/index.html file's permissions should be 0755 on all app servers.

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.


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 create inventory & playbook files

    cd   /home/thor/ansible 


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

thor@jump_host ~/ansible$ vi playbook.yml

- name: Install httpd and setup index.html

  hosts: stapp01, stapp02, stapp03

  become: yes

  tasks:

    - name: Install httpd

      package:

        name: httpd

        state: present

    - name: Start service httpd, if not started

      service:

        name: httpd

        state: started

    - name: Add content in index.html. Create file if it does not exist and set file attributes

      copy:

        dest: /var/www/html/index.html

        content: This is a Nautilus sample file, created using Ansible!

        mode: "0755"

        owner: apache

        group: apache

    - name: Update content in index.html

      lineinfile:

        path: /var/www/html/index.html

        insertbefore: BOF

        line: Welcome to xFusionCorp Industries!

 

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

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

 

PLAY [Install httpd and setup index.html] **********************************************************

 

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

ok: [stapp01]

ok: [stapp03]

ok: [stapp02]

 

TASK [Install httpd] *******************************************************************************

changed: [stapp03]

changed: [stapp02]

changed: [stapp01]

 

TASK [Start service httpd, if not started] *********************************************************

changed: [stapp01]

changed: [stapp02]

changed: [stapp03]

 

TASK [Add content in index.html. Create file if it does not exist and set file attributes] *********

changed: [stapp01]

changed: [stapp02]

changed: [stapp03]

 

TASK [Update content in index.html] ****************************************************************

changed: [stapp01]

changed: [stapp03]

changed: [stapp02]

 

PLAY RECAP *****************************************************************************************

stapp01                    : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

stapp02                    : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

stapp03                    : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 

thor@jump_host ~/ansible$


4. validate the task by curl on app server and check
      

thor@jump_host ~/ansible$ ansible all -a "cat /var/www/html/index.html" -i inventory

stapp02 | CHANGED | rc=0 >>

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!

stapp03 | CHANGED | rc=0 >>

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!

stapp01 | CHANGED | rc=0 >>

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ curl http://stapp01

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!thor@jump_host ~/ansible$

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ curl http://stapp02

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ curl http://stapp03

Welcome to xFusionCorp Industries!

This is a Nautilus sample file, created using Ansible!thor@jump_host ~/ansible$

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

 Restrict Cron Access Level 1 Linux KodeKloud Engineer Task Success