Content
Today I worked on an Ansible automation task that involved installing and configuring the Apache HTTP server on multiple application servers in the Stratos Datacenter. I also learned how to use Ansible's blockinfile module to create and manage file content while ensuring the correct ownership and permissions.
๐น What I Learned
Installing packages using Ansible
Managing services with the service module
Creating files using the blockinfile module
Automatically creating files that do not already exist
Setting file ownership and permissions using Ansible
๐น Task Requirement
As per the Nautilus DevOps team requirements, I needed to:
- Create an Ansible playbook:
/home/thor/ansible/playbook.yml
using the existing inventory file:
/home/thor/ansible/inventory
Install the httpd package on all application servers.
Ensure the httpd service is started and enabled.
Create the following file if it does not exist:
/var/www/html/index.html
using the blockinfile module.
The file should contain:
Welcome to XfusionCorp!
This is Nautilus sample file, created using Ansible!
Please do not modify this file manually!
- Set the ownership of the file to:
Owner : apache
Group : apache
- Set the file permissions to:
0655
The playbook must execute successfully using:
ansible-playbook -i inventory playbook.yml
without passing any additional arguments.
๐น Steps I Followed
1. Navigated to the Working Directory
I first moved to the Ansible working directory on the jump host.
cd /home/thor/ansible
This directory already contained the inventory file provided for the task.
2. Verified the Inventory
I checked the existing inventory.
cat inventory
Output:
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 inventory defines the three application servers and the SSH credentials Ansible uses to connect to them.
3. Verified the Initial State
Before creating the playbook, I connected to one application server to verify whether Apache was already installed.
ssh tony@stapp01
Then attempted to access Apache's default document root.
cd /var/www/html
Output:
-bash: cd: /var/www/html/: No such file or directory
Observation
Since the httpd package had not yet been installed, the default web root directory did not exist.
This confirmed that the playbook would need to install Apache before creating the web page.
After verifying this, I returned to the jump host.
4. Created the Playbook
I created the playbook.
vi playbook.yml
Added the following content:
---
- name: Install and configure Apache web server
hosts: all
become: yes
tasks:
- name: Install httpd package
ansible.builtin.package:
name: httpd
state: present
- name: Ensure httpd service is running and enabled
ansible.builtin.service:
name: httpd
state: started
enabled: yes
- name: Create and populate index.html
ansible.builtin.blockinfile:
path: /var/www/html/index.html
create: yes
owner: apache
group: apache
mode: '0655'
block: |
Welcome to XfusionCorp!
This is Nautilus sample file, created using Ansible!
Please do not modify this file manually!
Saved the file.
๐น Understanding the Playbook
Target Hosts
hosts: all
The playbook runs on every server defined in the inventory.
Privilege Escalation
become: yes
Installing packages, managing services, and modifying files under /var/www require administrative privileges.
Using become: yes allows Ansible to execute these tasks as the root user.
Package Module
package:
The package module provides a generic interface for package management.
It automatically uses the appropriate package manager on the target system (such as yum or dnf).
Service Module
service:
The service module manages system services.
In this task it ensures that:
Apache is running.
Apache starts automatically after system reboot.
Blockinfile Module
blockinfile:
The blockinfile module inserts, updates, or manages a block of text inside a file.
If the file does not exist, setting:
create: yes
allows Ansible to create it automatically.
Ownership
owner: apache
group: apache
These options ensure that the created file belongs to the Apache service account.
File Permissions
mode: '0655'
This sets the file permissions to:
-rw-r-xr-x
meaning:
Owner: read and write
Group: read and execute
Others: read and execute
Default Block Markers
The task specified not to use custom or empty markers.
Therefore, Ansible automatically inserts its default markers:
# BEGIN ANSIBLE MANAGED BLOCK
...
# END ANSIBLE MANAGED BLOCK
These markers help Ansible identify and update the managed block during future playbook runs.
5. Executed the Playbook
Ran:
ansible-playbook -i inventory playbook.yml
Output:
PLAY [Install and configure Apache web server]
TASK [Gathering Facts]
ok: [stapp01]
ok: [stapp02]
ok: [stapp03]
TASK [Install httpd package]
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
TASK [Ensure httpd service is running and enabled]
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
TASK [Create and populate index.html]
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
PLAY RECAP
stapp01 : ok=4 changed=3 failed=0
stapp02 : ok=4 changed=3 failed=0
stapp03 : ok=4 changed=3 failed=0
๐น Understanding the Output
Gathering Facts
Before executing any tasks, Ansible collected system information from every managed node.
Package Installation
changed
The changed status indicates that Apache was successfully installed.
Service Configuration
The service task started Apache and enabled it to start automatically after boot.
Blockinfile Task
The blockinfile module created the file and inserted the required content.
Play Recap
ok=4
changed=3
failed=0
Meaning:
Four tasks completed successfully.
Three tasks modified each server.
No failures occurred.
6. Validated the Configuration
After the playbook completed, I connected to one application server.
ssh tony@stapp01
Verified that the document root now existed.
cd /var/www/html
ls
Output:
index.html
Verified File Content
cat /var/www/html/index.html
Output:
# 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
This confirms that blockinfile inserted the required content along with its default markers.
Verified Ownership and Permissions
Executed:
ls -la /var/www/html
Output:
-rw-r-xr-x 1 apache apache ...
This confirmed:
Owner: apache
Group: apache
Permissions: 0655
Verified Apache Service
Checked the service status.
systemctl status httpd
Output showed:
Active: active (running)
This confirmed that the web server was running successfully.
๐น Verification
โ Apache installed successfully
โ Apache service started and enabled
โ
index.html created automatically
โ Required content added using blockinfile
โ
File owned by apache:apache
โ
File permissions set to 0655
โ Playbook executed successfully on all application servers
๐น My Understanding
This task helped me understand that Ansible is not only useful for installing software but also for managing configuration files. The blockinfile module makes it easy to insert and maintain blocks of text while preserving the rest of the file.
๐น What I Found Interesting
I found it interesting that with a single playbook, Ansible installed Apache, started the service, created a missing file, managed its content, and applied the correct ownership and permissions across all three application servers.
๐ Full notes: GitHub link
- #ansible-blockinfile-module
- #blockinfile
- #blockinfilemodule
- #ansible
- #ansible-playbook
- #ansible-module
- #ansible-adhoc
- #ansible-automation
- #ansible-project
- #ansible-inventory-file
- #ansible-article
- #ansible-devops
- #devops
- #devops-articles
- #devops-journey
- #devopscommunity
- #devops-trends
- #devops-tools
- #devops-engineer
- #devopsculture
- #devopsjourney
- #devopsproject
- #100daysofdevops
About the author
- Iโm currently working in DevOps and documenting my learning journey along the way. From CI/CD pipelines to cloud and containers, Iโm exploring how modern systems are built and deployed. This blog is where I share what I learn, break down concepts in simple terms, and track my progress. Learning one concept at a time, and trying to apply it practically.
Comments
Join the discussionNo comments yet. Be the first to comment.
100_days_of_devops
Documenting my 100 Days of DevOps journey with hands-on practice from KodeKloud, along with notes, commands, and mini projects