Developers are looking for dependencies to be installed and run on Nautilus app servers in Stratos DC. They have shared some requirements with the DevOps team. Because we are now managing packages installation and services management using Ansible, some playbooks need to be created and tested. As per details mentioned below please complete the task:
a. On jump host create an Ansible playbook /home/thor/ansible/playbook.yml and configure it to install httpd on all app servers.
b. After installation make sure to start and enable httpd service on all app servers.
c. The inventory /home/thor/ansible/inventory is already there on jump host.
d. Make sure user thor should be able to run the playbook on jump host.
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.
A practical walkthrough of automating package installation and service management across three application servers using a single Ansible playbook.
Introduction
In any DevOps workflow, you will frequently need to install packages and manage services across multiple servers. Doing this manually is error-prone and does not scale. Ansible provides a clean, declarative way to handle both tasks.
This article walks through a real lab exercise: installing the Apache HTTP Server (httpd) on three application servers, then starting and enabling the service so it persists across reboots. Every command and output shown here is from an actual run.
The Requirements
The task had four specific requirements:
- Create an Ansible playbook at
/home/thor/ansible/playbook.ymlthat installshttpdon all app servers. - After installation, start and enable the
httpdservice on all app servers. - Use the existing inventory file at
/home/thor/ansible/inventory. - The playbook must run with the exact command
ansible-playbook -i inventory playbook.ymlwithout any extra arguments.
Step 1: Navigate to the Working Directory
cd /home/thor/ansible
Verify the inventory file is present and readable:
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 is a flat inventory with three hosts. There are no group headers such as [app], so the playbook must target hosts using all or list them individually. Using hosts: all is the simplest approach here and matches the requirement to install on all app servers.
Step 2: Create the Playbook
Open the playbook file:
vi /home/thor/ansible/playbook.yml
Add the following content:
---
- name: Install and configure httpd
hosts: all
become: true
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Start and enable httpd service
systemd:
name: httpd
state: started
enabled: yes
Save the file and exit the editor.
Verify the file contents:
cat /home/thor/ansible/playbook.yml
Output:
---
- name: Install and configure httpd
hosts: all
become: true
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Start and enable httpd service
systemd:
name: httpd
state: started
enabled: yes
Understanding the Playbook
Each part of the playbook serves a specific purpose.
hosts: all targets every host in the inventory. Since the inventory contains only the three app servers, this effectively targets all app servers.
become: true enables privilege escalation. Installing packages and managing system services requires root privileges. Without this setting, the playbook would fail with permission errors.
yum module with state: present installs the httpd package if it is not already installed. If it is already present, Ansible does nothing. This is idempotent behavior.
systemd module with state: started and enabled: yes ensures the service is running right now and is configured to start automatically at boot. Both conditions are checked independently, so the module is idempotent in each case.
Step 3: Validate the Syntax
Before running the playbook, validate the YAML syntax:
ansible-playbook -i inventory playbook.yml --syntax-check
Output:
playbook: playbook.yml
A successful syntax check returns the playbook filename with no error messages.
Step 4: Run the Playbook
Execute the playbook exactly as the validation would:
ansible-playbook -i inventory playbook.yml
Output:
PLAY [Install and configure httpd] *******************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************
ok: [stapp01]
ok: [stapp03]
ok: [stapp02]
TASK [Install httpd package] *************************************************************************************************
changed: [stapp03]
changed: [stapp01]
changed: [stapp02]
TASK [Start and enable httpd service] ****************************************************************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]
PLAY RECAP *******************************************************************************************************************
stapp01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp03 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The play recap confirms the playbook ran successfully on all three servers. Each host shows changed=2, meaning both the package installation and the service configuration made actual changes. All hosts show failed=0.
Step 5: Verify the Package Installation
Confirm that httpd is installed on all servers using the RPM query:
ansible all -i inventory -a "rpm -q httpd"
Output:
stapp01 | CHANGED | rc=0 >>
httpd-2.4.62-17.el9.x86_64
stapp03 | CHANGED | rc=0 >>
httpd-2.4.62-17.el9.x86_64
stapp02 | CHANGED | rc=0 >>
httpd-2.4.62-17.el9.x86_64
The package name and version confirm that httpd is installed on all three servers. The operating system is clearly RHEL 9 or a compatible distribution based on the .el9 version suffix.
Step 6: Verify the Service Status
Check that the service is running and enabled using systemctl:
ansible all -i inventory -a "systemctl status httpd"
Output for stapp03:
stapp03 | CHANGED | rc=0 >>
* httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Tue 2026-11-17 10:08:28 UTC; 1min 13s ago
Docs: man:httpd.service(8)
Main PID: 19736 (httpd)
Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec"
Tasks: 177 (limit: 404712)
Memory: 14.9M
CPU: 102ms
CGroup: /system.slice/httpd.service
...
Nov 17 10:08:28 stapp03 systemd[1]: Starting The Apache HTTP Server...
Nov 17 10:08:28 stapp03 httpd[19736]: Server configured, listening on: port 80
Nov 17 10:08:28 stapp03 systemd[1]: Started The Apache HTTP Server.
Output for stapp01:
stapp01 | CHANGED | rc=0 >>
* httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Tue 2026-11-17 10:08:28 UTC; 1min 13s ago
...
Main PID: 14958 (httpd)
...
Nov 17 10:08:28 stapp01 systemd[1]: Starting The Apache HTTP Server...
Nov 17 10:08:28 stapp01 httpd[14958]: Server configured, listening on: port 80
Nov 17 10:08:28 stapp01 systemd[1]: Started The Apache HTTP Server.
Output for stapp02:
stapp02 | CHANGED | rc=0 >>
* httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Tue 2026-11-17 10:08:28 UTC; 1min 12s ago
...
Main PID: 12405 (httpd)
...
Nov 17 10:08:28 stapp02 systemd[1]: Starting The Apache HTTP Server...
Nov 17 10:08:28 stapp02 httpd[12405]: Server configured, listening on: port 80
Nov 17 10:08:28 stapp02 systemd[1]: Started The Apache HTTP Server.
Two lines in each output are important for validation:
-
Loaded: loaded (...; enabled; ...)confirms the service is enabled at boot. -
Active: active (running)confirms the service is currently running.
Each server also shows Server configured, listening on: port 80, which confirms the service is functional and accepting connections.
You may notice the following warning in the log output:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.244.29.196. Set the 'ServerName' directive globally to suppress this message.
This is a standard Apache informational message when no ServerName is configured in the httpd configuration. It does not prevent the service from starting or functioning and does not affect this task.
Step 7: Confirm Idempotency
One of the most important properties of a well-written Ansible playbook is idempotency. Running the playbook a second time should make no changes if the desired state is already achieved.
Run the playbook again:
ansible-playbook -i inventory playbook.yml
Output:
PLAY [Install and configure httpd] ***************************************************************************
TASK [Gathering Facts] ***************************************************************************************
ok: [stapp03]
ok: [stapp02]
ok: [stapp01]
TASK [Install httpd package] *********************************************************************************
ok: [stapp03]
ok: [stapp01]
ok: [stapp02]
TASK [Start and enable httpd service] ************************************************************************
ok: [stapp03]
ok: [stapp01]
ok: [stapp02]
PLAY RECAP ***************************************************************************************************
stapp01 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp03 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
All tasks report ok instead of changed. The changed=0 in the play recap confirms the playbook made no modifications on the second run. This demonstrates that the playbook is idempotent and safe to run repeatedly in automation pipelines.
Why Use the systemd Module Instead of the service Module
Both modules work for starting and enabling services, but they differ in scope.
The service module is a generic wrapper that auto-detects the init system on the target. It works across older and newer Linux distributions.
The systemd module is specifically designed for systems that use systemd as the init system. It provides more explicit control and reliable behavior on modern distributions such as RHEL 7 and later, Ubuntu 16.04 and later, and most container-based environments.
Since the app servers in this lab run RHEL 9, the systemd module is the correct choice.
Summary of Key Concepts
| Concept | What It Does in This Playbook |
|---|---|
hosts: all |
Targets every host in the inventory |
become: true |
Enables privilege escalation for root-required operations |
yum: state=present |
Installs the package if not already installed |
systemd: state=started |
Ensures the service is currently running |
systemd: enabled=yes |
Ensures the service starts automatically at boot |
| Idempotency | Re-running the playbook produces no changes |
Final Playbook
---
- name: Install and configure httpd
hosts: all
become: true
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Start and enable httpd service
systemd:
name: httpd
state: started
enabled: yes
Conclusion
This exercise demonstrates how Ansible simplifies the common task of installing packages and managing services across multiple servers. The playbook is short, readable, and reusable.
Key takeaways:
- The
yummodule handles package installation idempotently. - The
systemdmodule handles both the running state and the boot-time enablement in a single task. - Running the playbook twice proves idempotency, which is essential for production automation.
- Verification with
rpm -qandsystemctl statusprovides confidence that the desired state was actually achieved on every host.
This pattern generalizes to any package and service combination. Replace httpd with nginx, postgresql, or any other service name, and the same structure applies.
Top comments (0)