What is Ansible?
Ansible is an open-source IT automation platform created by Michael DeHaan in 2012 and now sponsored by Red Hat. It's designed to solve the complexity of managing modern IT infrastructure by providing a radically simple approach to automation. Unlike many automation tools, Ansible is agentless — it uses SSH to communicate with remote systems without requiring any software installation on target machines.
The platform handles configuration management, application deployment, cloud provisioning, ad-hoc task execution, network automation, and multi-node orchestration. What sets Ansible apart is its use of YAML-based playbooks that are designed to be both human and machine readable, making automation accessible to both developers and system administrators.
Getting Started
Installing Ansible is straightforward across different platforms. The latest version 2.20.3 requires Python 3.12 or higher.
Installation via pip
pip install ansible-coreUbuntu/Debian
sudo apt update
sudo apt install ansibleCentOS/RHEL/Fedora
sudo dnf install ansiblemacOS
brew install ansibleAfter installation, verify the setup:
ansible --version
ansible-playbook --versionUsage & Practical Examples
Ansible operates through an inventory file that defines your infrastructure and playbooks that describe desired configurations.
Basic Inventory Setup
Create an inventory file hosts.ini:
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
db2.example.com
[all:vars]
ansible_user=admin
ansible_ssh_private_key_file=~/.ssh/id_rsaSimple Ad-Hoc Commands
Test connectivity to all hosts:
ansible all -i hosts.ini -m pingInstall a package on web servers:
ansible webservers -i hosts.ini -m apt -a "name=nginx state=present" --becomeSample Playbook
Create a playbook webserver.yml to configure nginx:
---
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Start and enable nginx
systemd:
name: nginx
state: started
enabled: yes
- name: Copy custom index.html
copy:
content: "Welcome to {{ inventory_hostname }}
"
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
- name: Ensure nginx is running
service:
name: nginx
state: startedRun the playbook:
ansible-playbook -i hosts.ini webserver.ymlUsing Roles for Complex Deployments
For larger deployments, organize code into roles:
ansible-galaxy init roles/common
ansible-galaxy init roles/webserver
ansible-galaxy init roles/database




