Ansible is the simplest way to automate server configuration. No agents to install, no complex setup โ just SSH and YAML. This guide takes you from zero to managing production infrastructure.
Installation (60 Seconds)
# Ubuntu/Debian
sudo apt update && sudo apt install -y ansible
# RHEL/CentOS
sudo dnf install -y ansible-core
# pip (any OS)
pip3 install ansible
# Verify
ansible --version
Your First Inventory
# inventory.ini
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com ansible_user=admin
[production:children]
webservers
dbservers
[all:vars]
ansible_python_interpreter=/usr/bin/python3
Ad-Hoc Commands
# Test connectivity
ansible all -i inventory.ini -m ping
# Run command on all servers
ansible all -m shell -a "uptime && free -h"
# Install package on web servers
ansible webservers -m apt -a "name=nginx state=present" -b
# Copy file
ansible all -m copy -a "src=./config.conf dest=/etc/app/config.conf" -b
Your First Playbook
---
# deploy-webserver.yml
- name: Deploy Web Server
hosts: webservers
become: yes
vars:
app_port: 8080
app_user: deploy
tasks:
- name: Update packages
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install required packages
apt:
name:
- nginx
- certbot
- python3-certbot-nginx
state: present
- name: Create app user
user:
name: "{{ app_user }}"
shell: /bin/bash
create_home: yes
- name: Deploy Nginx config
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/sites-available/default
notify: Reload Nginx
- name: Ensure Nginx is running
service:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
service:
name: nginx
state: reloaded
Using Ansible Vault
# Create encrypted vars file
ansible-vault create vars/secrets.yml
# Content:
db_password: "super-secret-password"
api_key: "sk-1234567890"
# Use in playbook
- name: Deploy with secrets
hosts: all
become: yes
vars_files:
- vars/secrets.yml
tasks:
- name: Configure database
template:
src: db.conf.j2
dest: /etc/app/db.conf
# Run with vault password
ansible-playbook site.yml --ask-vault-pass
Roles for Reusable Code
# Create role structure
ansible-galaxy init roles/webserver
# Use in playbook
- hosts: webservers
become: yes
roles:
- webserver
- role: monitoring
vars:
monitoring_port: 9090
๐ฅ Download the Ansible Cheat Sheet
Get our free Ansible quick reference PDF โ all essential commands, modules, and playbook patterns on one page.
Download Free Cheat Sheets โWith Ansible, you can go from manually SSHing into servers to fully automated infrastructure in an afternoon. Start with ad-hoc commands, graduate to playbooks, then organize with roles. Your future self will thank you.