In today's fast-paced IT environments, manual server management is not only time-consuming but also prone to errors.
Ansible, a powerful open-source automation tool, helps simplify server provisioning, configuration, and maintenanceโallowing
system administrators and DevOps teams to manage infrastructure effortlessly.
Whether you're managing a single server or thousands across multiple environments, Ansible's agentless architecture and simple YAML-based playbooks make automation accessible for teams of all sizes.
๐ ๏ธ Getting Started with Ansible
๐ฅ๏ธ 1. Install Ansible
# For Ubuntu/Debian
sudo apt update
sudo apt install ansible -y
# Verify installation
ansible --version
Output Example:
ansible 2.14.0
python version = 3.10.6
๐ง 2. Set Up the Inventory File
Create an inventory file to define your target servers:
# Create an inventory file
nano /etc/ansible/hosts
Sample Inventory File:
[web_servers]
192.168.1.101
192.168.1.102
[db_servers]
192.168.1.201
[all:vars]
ansible_user=admin
ansible_ssh_private_key_file=/home/admin/.ssh/id_rsa
๐ 3. Run Your First Ansible Command
Use the following command to test connectivity:
ansible all -m ping
Expected Output:
192.168.1.101 | SUCCESS => {
"ping": "pong"
}
๐ Automating Server Maintenance with Ansible
๐ ๏ธ Playbook Example: Automate System Updates
---
- name: Perform System Maintenance
hosts: all
become: true
tasks:
- name: Update system packages
apt:
update_cache: yes
upgrade: dist
- name: Remove unnecessary packages
apt:
autoremove: yes
- name: Clean up APT cache
apt:
autoclean: yes
- name: Reboot servers if needed
command: shutdown -r now
async: 1
poll: 0
ignore_errors: yes
Run the playbook:
ansible-playbook maintenance.yml