Ansible
Ansible is an agentless automation tool for configuration management, application deployment, and task automation. It uses SSH to connect to managed nodes.
Ansible vs Other Tools
Agent-based (Puppet, Chef): Agentless (Ansible):
ββββββββββββββββββββββ ββββββββββββββββββββββ
β Install agent on β β No agent needed β
β every server β β Uses SSH β
β β β β
β ββββ ββββ βββββ β ββββ ββββ βββββ
β βA β βA β βA ββ β β β β β β ββ
β ββββ ββββ βββββ β ββββ ββββ βββββ
β Servers with agentsβ β Servers (plain) β
ββββββββββββββββββββββ ββββββββββββββββββββββ
Ansible Playbook
# deploy-app.yml
---
- name: Deploy web application
hosts: webservers
become: yes
vars:
app_version: "2.1.0"
app_port: 8080
tasks:
- name: Install dependencies
apt:
name:
- nginx
- python3
- python3-pip
state: present
update_cache: yes
- name: Copy application code
copy:
src: ./dist/
dest: /opt/myapp/
owner: www-data
group: www-data
- name: Configure nginx
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/sites-available/myapp
notify: Reload nginx
- name: Ensure services are running
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- nginx
- myapp
handlers:
- name: Reload nginx
service:
name: nginx
state: reloaded
Ansible Inventory
# inventory/hosts.yml
all:
children:
webservers:
hosts:
web1.example.com:
web2.example.com:
databases:
hosts:
db1.example.com:
db2.example.com:
vars:
ansible_user: deploy
ansible_ssh_private_key_file: ~/.ssh/deploy_key
Key Ansible Concepts
- Playbooks β YAML files defining automation tasks
- Roles β Reusable collections of tasks, handlers, and templates
- Inventory β List of servers to manage
- Modules β Built-in units of work (apt, copy, service, etc.)
- Galaxy β Community marketplace for Ansible roles