Ansible is an open-source IT automation tool that simplifies configuration management, application deployment, and orchestration. Written in Python, it uses SSH for agentless communication with remote systems.
- Agentless Architecture: No client installation required
- Push-based Configuration: Master server pushes changes to nodes
- YAML-based Playbooks: Human-readable automation scripts
- Extensible Modules: 1000+ built-in modules for various tasks
- Multi-Platform Support: Manages Linux, Windows, network devices
- Idempotent Operations: Safe to run multiple times
-
Inventory (
/etc/ansible/hosts
):[webservers] web01 ansible_host=192.168.1.1 web02 ansible_host=192.168.1.2
-
Modules (Execution units):
ansible all -m apt -a "name=nginx state=present"
-
Playbooks (YAML automation files):
- name: Install Apache hosts: webservers tasks: - name: Install package apt: name: apache2 state: present
-
Master Node Setup:
sudo apt update sudo apt install ansible ssh-keygen -t rsa ssh-copy-id user@remote-host
-
Verify Installation:
ansible --version ansible localhost -m ping
-
Sample Inventory:
sudo nano /etc/ansible/hosts
Command | Description |
---|---|
ansible all -m ping |
Test node connectivity |
ansible web -m apt -a "name=nginx state=absent" -b |
Remove package with sudo |
ansible db -m copy -a "src=app.conf dest=/etc/" |
Copy configuration files |
ansible all -m command -a "uptime" |
Check system uptime |
ansible web -m service -a "name=apache2 state=restarted" |
Restart services |
webserver.yaml
:
- name: Configure Web Cluster
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: latest
- name: Start Service
service:
name: apache2
state: started
enabled: yes
Execution Commands:
ansible-playbook --syntax-check webserver.yaml # Validate syntax
ansible-playbook webserver.yaml --ask-become-pass # Run with sudo
File | Purpose | Location |
---|---|---|
Main Config | Global settings | /etc/ansible/ansible.cfg |
Inventory | Host definitions | /etc/ansible/hosts (default) |
Roles | Reusable components | /etc/ansible/roles/ |
- Use version control for playbooks
- Implement Ansible Vault for secrets
- Use tags for selective execution
- Create modular roles for reuse
- Test with
--check
mode before execution