Ansible Blockinfile Module

Ticker

6/recent/ticker-posts

Ansible Blockinfile Module

 Question The Nautilus DevOps team wants to install and set up a simple httpd web server on all app servers in Stratos DC. Additionally, they want to deploy a sample web page for now 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. Additionally, make sure its service should up and running.

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

Welcome to XfusionCorp!

This is Nautilus sample file, created using Ansible!

Please do not modify this file manually!

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 0644 on all app servers.

Note:

i. 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.

ii. Do not use any custom or empty marker for blockinfile module.

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    

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

thor@jump_host ~/ansible$ ll

total 8

-rw-r--r-- 1 thor thor  36 Jul  5 14:24 ansible.cfg

-rw-r--r-- 1 thor thor 237 Jul  5 14:24 inventory

thor@jump_host ~/ansible$


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

thor@jump_host ~/ansible$ vi playbook.yml

thor@jump_host ~/ansible$ cat 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 block in index.html and set permissions

       blockinfile:

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

         create: yes

         block: |

           Welcome to XfusionCorp!

           This is Nautilus sample file, created using Ansible!

           Please do not modify this file manually!

         owner: apache

         group: apache

         mode: "0644"

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 [Install httpd and setup index.html] ***********************************************************************************

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

ok: [stapp01]

ok: [stapp02]

ok: [stapp03]

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

changed: [stapp01]

changed: [stapp03]

changed: [stapp02]

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

changed: [stapp01]

changed: [stapp03]

changed: [stapp02]

 TASK [Add content block in index.html and set permissions] ******************************************************************

changed: [stapp02]

changed: [stapp03]

changed: [stapp01]

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

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

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

stapp03                    : ok=4    changed=3    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 'ls -l /var/www/html/' -i inventory

stapp01 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 apache apache 179 Jul  5 14:47 index.html

stapp03 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 apache apache 179 Jul  5 14:47 index.html

stapp02 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 apache apache 179 Jul  5 14:47 index.html

thor@jump_host ~/ansible$

    

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

# BEGIN ANSIBLE MANAGED BLOCK

Welcome to XfusionCorp!

This is Nautilus sample file, created using Ansible!

Please do not modify this file manually!

# END ANSIBLE MANAGED BLOCK

thor@jump_host ~/ansible$

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

# BEGIN ANSIBLE MANAGED BLOCK

Welcome to XfusionCorp!

This is Nautilus sample file, created using Ansible!

Please do not modify this file manually!

# END ANSIBLE MANAGED BLOCK

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

# BEGIN ANSIBLE MANAGED BLOCK

Welcome to XfusionCorp!

This is Nautilus sample file, created using Ansible!

Please do not modify this file manually!

# END ANSIBLE MANAGED BLOCK

 


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