Introduction
Automation is at the heart of modern DevOps practices, and Ansible continues to be a go-to solution for configuration management. Recently, the Nautilus Application Development team requested that specific packages be installed on all application servers in the Stratos Datacenter. Rather than performing this task manually on each server, we leveraged Ansible to ensure consistency and efficiency.
This blog post details how we created an inventory file and Ansible playbook to install the chrony package (a time synchronization tool) across all application servers.
The Challenge
The Nautilus Application development team wanted to test some applications on app servers in Stratos Datacenter. They shared some pre-requisites with the DevOps team, and packages need to be installed on app servers. Since we are already using Ansible for automating such tasks, please perform this task using Ansible as per details mentioned below:
Create an inventory file
/home/thor/playbook/inventoryonjump hostand add all app servers in it.Create an Ansible playbook
/home/thor/playbook/playbook.ymlto installchronypackage onall app serversusing Ansibleyummodule.Make sure user
thorshould be able to run the playbook onjump 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.
The Solution
Step 1: Creating the Inventory File
First, we created the inventory file at /home/thor/playbook/inventory containing all three application servers:
[app_servers]
stapp01 ansible_user=tony ansible_ssh_pass=Ir0nM@n ansible_ssh_common_args='-o StrictHostKeyChecking=no'
stapp02 ansible_user=steve ansible_ssh_pass=Am3ric@ ansible_ssh_common_args='-o StrictHostKeyChecking=no'
stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n ansible_ssh_common_args='-o StrictHostKeyChecking=no'
Each server entry includes:
- Hostname alias
- SSH user credentials
- Password for authentication
- SSH connection arguments (host key checking disabled for seamless connectivity)
Step 2: Creating the Ansible Playbook
We created the playbook at /home/thor/playbook/playbook.yml with the following structure:
---
- name: Install chrony package on all app servers
hosts: app_servers
gather_facts: no
tasks:
- name: Install chrony using yum module
yum:
name: chrony
state: present
become: yes
Breakdown of the Playbook:
-
hosts: app_servers: Targets all servers in the inventory group -
gather_facts: no: Skips fact gathering for faster execution -
yummodule: The native package manager for RHEL-based systems -
state: present: Ensures the package is installed -
become: yes: Uses sudo privileges for package installation
Step 3: Running the Playbook
With both files in place, we executed the playbook:
cd /home/thor/playbook
ansible-playbook -i inventory playbook.yml
Expected Output:
PLAY [Install chrony package on all app servers] ********************************
TASK [Install chrony using yum module] *****************************************
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]
PLAY RECAP *********************************************************************
stapp01 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp02 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp03 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The changed status indicates the package was successfully installed on all servers.
Key Learnings
1. Inventory Management
Centralizing server information in an inventory file simplifies management and ensures consistency across playbook executions. The file format supports grouping, variables, and connection parameters, making it highly flexible.
2. The Yum Module
Ansible's yum module is straightforward yet powerful. It handles package installation, updates, and removals across RHEL-based systems. The state: present parameter ensures idempotency—running the playbook multiple times won't cause errors or reinstall the package unnecessarily.
3. Privilege Escalation
Using become: yes allows the playbook to perform administrative tasks without requiring manual sudo intervention on each server. This is essential for package installations and other system-level operations.
4. Idempotency in Action
One of Ansible's greatest strengths is idempotency. If we run the playbook again, it will report ok instead of changed, confirming the package is already installed. This predictability is invaluable in production environments.
Verification
To confirm the installation was successful, we can run:
ansible -i inventory app_servers -m shell -a "rpm -q chrony"
This should return the installed package version on each server, confirming the package is present and ready for use.
Conclusion
The task was completed successfully. The chrony package is now installed on all application servers in the Stratos DC, and the playbook is ready for future use. This approach saved significant time compared to manual installation and ensures consistency across all servers.
The Nautilus Application Development team can now proceed with their testing, knowing that the required package is in place on all servers. Ansible's simplicity and power continue to make it an essential tool in our DevOps toolkit.
Top comments (0)