← Back to list

KodeKloud Engineer Day 91: Ansible Lineinfile Module.

Hey Everyone! I have taken up the kodekloud engineer daily challenge [100 days of Devops] and through this series, we shall slowly but…

Jane Mils · 2026-05-28 12:41 · 0 claps · 5.8 min read
#kodekloud #kodekloudengineer #devops #ansible #ansible-playbook
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

KodeKloud Engineer Day 91: Ansible Lineinfile Module.

Hey Everyone! I have taken up the kodekloud engineer daily challenge [100 days of Devops] and through this series, we shall slowly but steadily start solving the daily challenges.

Here’s the link to get started with the challenge: https://engineer.kodekloud.com/practice

Day #91 Task: Ansible Lineinfile Module.

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. Therefore, write the required playbook to complete this task as per details mentioned below.

We already have an inventory file under /home/thor/ansible directory on jump host. Write a playbook playbook.yml under /home/thor/ansible directory on jump host itself. Using the playbook perform below given tasks:

  • 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 Nautilus Group!

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

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.

Day #91 Task: Ansible Lineinfile Module.

Day #91 Task: Ansible Lineinfile Module.

What is the lineinfile module and what is it’s purpose?

The lineinfile module in Ansible is used to add, modify, or ensure the presence of a specific line inside a file. Its purpose is to manage single-line changes in files safely and automatically without editing the whole file.

Common uses :-

  • Updating configuration files.
  • Adding environment variables.
  • Changing SSH settings.
  • Inserting one specific line.
  • Ensuring duplicate lines are not created.

Step-1: Verify the existing inventory file.

Let’s just check the content of the existing inventory file.

thor@jump-host ~/ansible$ cat inventory 
stapp01 ansible_host=stapp01 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=stapp02 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=stapp03 ansible_ssh_pass=BigGr33n ansible_user=banner

This means that the playbook that we’ll create will run on all the three app-servers.

Step-2: Create the playbook with the defined requirements.

As seen in the problem statement, there are 3 tasks to include in the playbook.

  • To install the httpd service using the yum package.
  • To run the httpd service after successful installation.
  • To add the content in the */var/www/html/index.htmlfile using the `lineinfile* module in Ansible. This file must have it’s permission set to0655`and the owner/group must be set to *apache*. The content must be added in the following manner:
  • The file must be created with : “*This is a Nautilus sample file, created using Ansible!*” as the content of the file.
  • Using the*lineinfile module, this “`Welcome to Nautilus Group!`*” must be added to the top of the created file.
thor@jump-host ~$ vi /home/thor/ansible/playbook.yml 

# Content of the playbook.yml file.
---
- hosts: all
  become: yes

  tasks:
    - name: Install httpd package.
      yum:
        name: httpd
        state: present
    - name: Starting the httpd service.
      service:
        name: httpd
        state: started
        enabled: yes
    - name: Create index.html with initial content
      copy:
        dest: /var/www/html/index.html
        content: |
          This is a Nautilus sample file, created using Ansible!

    - name: Add welcome line at the top of file
      lineinfile:
        path: /var/www/html/index.html
        line: Welcome to Nautilus Group!
        insertbefore: BOF
        mode: '0655'
        owner: "apache"
        group: "apache"

Step-3: Validate the execution of the playbook against the inventory file.

Check in all the app servers, before executing the playbook file, if any such file exists.

# Check in the app-server 01 if any files exists in path /var/www/html. Repeat the same for the other app servers.
thor@jump-host ~$ ssh tony@stapp01
The authenticity of host 'stapp01 (10.244.234.239)' can't be established.
ED25519 key fingerprint is SHA256:MAqLyBAGdPiitFRZhezMjOXLKeb1PSuaeCT+4HBtuuc.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp01' (ED25519) to the list of known hosts.
tony@stapp01's password: 
[tony@stapp01 ~]$ sudo systemctl status httpd
Unit httpd.service could not be found.
[tony@stapp01 ~]$ ls /var/www/html/index.html
/var/www/html/index.html
[tony@stapp01 ~]$ cat /var/www/html/index.html
This is KodeKloud Ansible Lab !
# Execute the playbook against the inventory and check if the file /var/www/html/index.html is created in all the app servers and check the permissions and the user/groups ownerships.
thor@jump-host ~/ansible$ ansible-playbook -i inventory playbook.yml

PLAY [all] ******************************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [stapp03]
ok: [stapp01]
ok: [stapp02]

TASK [Install httpd package.] ***********************************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]

TASK [Starting the httpd service.] ******************************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]

TASK [Create index.html with initial content] *******************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]

TASK [Add welcome line at the top of file] **********************************************
changed: [stapp03]
changed: [stapp02]
changed: [stapp01]

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 
# Check in the app-server 01 if the file is created in path /var/www/html with the correct requirements. Repeat the same for the other app servers.
# In App Server 01.
[tony@stapp01 ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2026-05-25 10:13:11 UTC; 1min 15s ago
       Docs: man:httpd.service(8)
   Main PID: 26398 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/s>
      Tasks: 177 (limit: 404712)
     Memory: 14.9M
        CPU: 109ms
     CGroup: /system.slice/httpd.service
             ├─26398 /usr/sbin/httpd -DFOREGROUND
             ├─26633 /usr/sbin/httpd -DFOREGROUND
             ├─26637 /usr/sbin/httpd -DFOREGROUND
             ├─26643 /usr/sbin/httpd -DFOREGROUND
             └─26652 /usr/sbin/httpd -DFOREGROUND

May 25 10:13:11 stapp01 systemd[1]: Starting The Apache HTTP Server...
May 25 10:13:11 stapp01 httpd[26398]: AH00558: httpd: Could not reliably determine the s>
May 25 10:13:11 stapp01 httpd[26398]: Server configured, listening on: port 80
May 25 10:13:11 stapp01 systemd[1]: Started The Apache HTTP Server.
...skipping...
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2026-05-25 10:13:11 UTC; 1min 15s ago
       Docs: man:httpd.service(8)
   Main PID: 26398 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/s>
      Tasks: 177 (limit: 404712)
     Memory: 14.9M
        CPU: 109ms
     CGroup: /system.slice/httpd.service
             ├─26398 /usr/sbin/httpd -DFOREGROUND
             ├─26633 /usr/sbin/httpd -DFOREGROUND
             ├─26637 /usr/sbin/httpd -DFOREGROUND
             ├─26643 /usr/sbin/httpd -DFOREGROUND
             └─26652 /usr/sbin/httpd -DFOREGROUND

May 25 10:13:11 stapp01 systemd[1]: Starting The Apache HTTP Server...
May 25 10:13:11 stapp01 httpd[26398]: AH00558: httpd: Could not reliably determine the s>
May 25 10:13:11 stapp01 httpd[26398]: Server configured, listening on: port 80
May 25 10:13:11 stapp01 systemd[1]: Started The Apache HTTP Server.

[tony@stapp01 ~]$ cat /var/www/html/index.html
Welcome to Nautilus Group!
This is a Nautilus sample file, created using Ansible!

# Check the permission and the ownership of the file.
[tony@stapp01 ~]$ ls -la /var/www/html/index.html
-rw-r-xr-x 1 apache apache 82 May 25 10:22 /var/www/html/index.html

# In App Server 02.
thor@jump-host ~$ ssh steve@stapp02
steve@stapp02's password: 
Last login: Mon May 25 10:13:14 2026 from 10.244.29.238
[steve@stapp02 ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2026-05-25 10:13:11 UTC; 4min 57s ago
       Docs: man:httpd.service(8)
   Main PID: 23489 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/s>
      Tasks: 177 (limit: 404712)
     Memory: 14.9M
        CPU: 232ms
     CGroup: /system.slice/httpd.service
             ├─23489 /usr/sbin/httpd -DFOREGROUND
             ├─23664 /usr/sbin/httpd -DFOREGROUND
             ├─23668 /usr/sbin/httpd -DFOREGROUND
             ├─23672 /usr/sbin/httpd -DFOREGROUND
             └─23673 /usr/sbin/httpd -DFOREGROUND

May 25 10:13:11 stapp02 systemd[1]: Starting The Apache HTTP Server...
May 25 10:13:11 stapp02 httpd[23489]: AH00558: httpd: Could not reliably determine the s>
May 25 10:13:11 stapp02 httpd[23489]: Server configured, listening on: port 80
May 25 10:13:11 stapp02 systemd[1]: Started The Apache HTTP Server.

[steve@stapp02 ~]$ cat /var/www/html/index.html
Welcome to Nautilus Group!
This is a Nautilus sample file, created using Ansible!

[steve@stapp02 ~]$ ls -la /var/www/html/index.html
-rw-r-xr-x 1 apache apache 82 May 25 10:22 /var/www/html/index.html

# In App Server 03.
thor@jump-host ~$ ssh banner@stapp03
banner@stapp03's password: 
Last login: Mon May 25 10:13:14 2026 from 10.244.29.238
[banner@stapp03 ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2026-05-25 10:13:11 UTC; 6min ago
       Docs: man:httpd.service(8)
   Main PID: 24037 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/s>
      Tasks: 177 (limit: 404712)
     Memory: 14.9M
        CPU: 268ms
     CGroup: /system.slice/httpd.service
             ├─24037 /usr/sbin/httpd -DFOREGROUND
             ├─24127 /usr/sbin/httpd -DFOREGROUND
             ├─24132 /usr/sbin/httpd -DFOREGROUND
             ├─24133 /usr/sbin/httpd -DFOREGROUND
             └─24136 /usr/sbin/httpd -DFOREGROUND

May 25 10:13:11 stapp03 systemd[1]: Starting The Apache HTTP Server...
May 25 10:13:11 stapp03 httpd[24037]: AH00558: httpd: Could not reliably determine the s>
May 25 10:13:11 stapp03 httpd[24037]: Server configured, listening on: port 80
May 25 10:13:11 stapp03 systemd[1]: Started The Apache HTTP Server.

[banner@stapp03 ~]$  cat /var/www/html/index.html
Welcome to Nautilus Group!
This is a Nautilus sample file, created using Ansible!

[banner@stapp03 ~]$ ls -la /var/www/html/index.html
-rw-r-xr-x 1 apache apache 82 May 25 10:22 /var/www/html/index.html

That’s it for Day #91. Let’s hop on to the next challenge! See you there! Toodaloo!


메타데이터
post_id
ea71cfe9b3a8
slug
kodekloud-engineer-day-91-ansible-lineinfile-module-ea71cfe9b3a8
url
https://medium.com/@janemils/kodekloud-engineer-day-91-ansible-lineinfile-module-ea71cfe9b3a8
canonical_url
https://medium.com/@janemils/kodekloud-engineer-day-91-ansible-lineinfile-module-ea71cfe9b3a8
author_url
https://medium.com/@janemils
status
ok
fetched_at
2026-06-21 19:25:17