Day 10 - Configuration Management with Ansible
What is Ansible?
Ansible is an open-source configuration management, deployment, and automation tool developed by Red Hat.
It helps system administrators automate repetitive tasks like software installation, updates, configuration, and orchestration across multiple servers β without needing to log in to each server manually.
π§ Real-World Scenario
A system administrator manages hundreds of servers with different operating systems β Ubuntu, CentOS, and Windows.
Common tasks include:
OS updates
Applying security patches
Installing software (e.g., Git, databases)
β Problem
Doing this manually on each server is time-consuming.
Scripts were used (e.g., PowerShell for Windows, Bash for Linux), but maintaining them across multiple OS types was difficult.
With cloud and microservices, the number of servers grew drastically β scripts became inefficient.
β Solution β Configuration Management Tools
Tools like Puppet, Chef, and Ansible emerged to automate configuration tasks.
Among these, Ansible became the most popular due to its simplicity, agentless architecture, and YAML-based playbooks.
βοΈ Why Ansible is Better than Puppet
| Feature | Puppet | Ansible |
| Mechanism | Pull (agents pull config from master) | Push (controller pushes config) |
| Architecture | Master-Agent setup required | Agentless (uses SSH) |
| Ease of Setup | Complex (needs master/agent setup) | Simple (just IPs in inventory) |
| Windows Support | Limited | Better |
| Language | Puppet DSL | YAML (easier and readable) |
| Dynamic Inventory | Manual updates required | Auto-detects new hosts dynamically |
β οΈ Limitations of Ansible
Windows support is still not fully seamless
Debugging is not very intuitive
Performance may lag when managing thousands of servers
βοΈ Ansible Installation & Setup
To start using Ansible:
You need two servers:
Control Node β where Ansible is installed
Managed Node(s) β target servers
π Passwordless Authentication
On Ansible server:
ssh-keygen cat ~/.ssh/id_rsa.pubOn Target server:
- Add copied key to authorized_keys
vi ~/.ssh/authorized_keys
ποΈ Inventory File
The inventory file contains IP addresses or hostnames of target servers.
Default path:
/etc/ansible/hostsYou can also use a custom file via
-iflag.
Example:
[webservers]
192.168.10.10
192.168.10.11
[dbservers]
192.168.20.10
192.168.20.11
β‘ Ad-hoc Commands
Used for quick, one-time tasks (no need for a playbook).
Syntax:
ansible -i inventory_file <host/group> -m <module> -a "<command>"
Examples:
ansible all -m shell -a "uptime"
ansible webservers -m shell -a "nproc"
π Ansible Playbook
For complex, multi-step tasks, use playbooks written in YAML.
Example: Installing and starting Nginx
---
- name: Install and start Nginx
hosts: all
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx service
service:
name: nginx
state: started
Run the playbook:
ansible-playbook -i inventory_file playbook.yml
Use -v for verbose output.
π§© Ansible Roles
Roles help organize and structure large playbooks into reusable components.
Create a role:
ansible-galaxy init kubernetes
Structure created:
kubernetes/
βββ defaults/ # Default variables
βββ files/ # Static files to copy
βββ handlers/ # Handlers (e.g., restart service)
βββ meta/ # Metadata about the role
βββ tasks/ # Main tasks go here
βββ templates/ # Jinja2 templates
βββ tests/ # Test playbooks
βββ vars/ # Variable definitions
Write your playbook logic in tasks/main.yml.
π Ansible Vault
Used to encrypt sensitive information like passwords, API keys, or AWS credentials in playbooks.
Scenario:
You have AWS credentials inside a playbook β you canβt push it to GitHub as plain text.
Commands:
Create an encrypted file
ansible-vault create secrets.ymlEdit an existing vault file
ansible-vault edit secrets.ymlEncrypt an existing file
ansible-vault encrypt playbook.ymlDecrypt a file
ansible-vault decrypt playbook.ymlRun playbook with vault password
ansible-playbook playbook.yml --ask-vault-pass
Summary Table
| Concept | Description |
| Ansible | Agentless configuration management tool (push model) |
| Inventory | List of managed nodes |
| Ad-hoc Commands | One-time actions on servers |
| Playbook | YAML file containing tasks |
| Roles | Reusable and modular playbook structure |
| Vault | Encrypt sensitive data |
| Dynamic Inventory | Auto-detect infrastructure (e.g., AWS EC2) |