π§ Ansible Complete Reference Guide
Authorβs Note:
This guide is built from a real automation setup using Ansible for managing Proxmox and Terraform-created environments.
It is written in a narrative, KB-style tone to serve as both a learning and reference resource.
βοΈ Introduction: Why Ansible
Ansible is an open-source configuration management and automation tool developed by Red Hat.
It automates software provisioning, configuration, and orchestration across servers β all using simple YAML playbooks.
Key benefits:
- Agentless β no software needed on the target systems.
- Uses SSH or WinRM for connectivity.
- Idempotent β only applies changes if required.
- Human-readable YAML syntax.
π‘ Tip: Ansible is most powerful when combined with Terraform β Terraform provisions infrastructure, and Ansible configures it.
π§° Installing Ansible
On Ubuntu / Debian
sudo apt update && sudo apt install ansible -y
Verify installation:
ansible --version
ansible-config list
ansible-doc -l
Recommended Directory Layout
/home/automation/ansible/
βββ ansible.cfg
βββ inventory.ini
βββ playbooks/
β βββ site.yml
βββ roles/
π‘ Keep your playbooks, inventory, and roles neatly separated. This structure scales well.
π§± Understanding Core Components
| Component | Description |
|---|---|
| Inventory | List of target machines |
| Playbook | YAML file defining automation tasks |
| Module | Unit of work (e.g., apt, copy, user) |
| Role | Reusable, organized structure of tasks and files |
| Handler | Executes actions after a change (e.g., restart a service) |
Example Inventory:
[web]
10.0.4.10
10.0.4.11
[db]
10.0.4.20
Example Task:
- name: Update system packages
apt:
update_cache: yes
upgrade: dist
βοΈ Ansible Configuration File (ansible.cfg)
Example configuration:
[defaults]
inventory = /home/automation/ansible/inventory.ini
remote_user = automation
host_key_checking = False
retry_files_enabled = False
forks = 10
timeout = 30
[ssh_connection]
pipelining = True
control_path = ~/.ssh/ansible-%%r@%%h:%%p
Useful commands:
ansible-config view
ansible-config dump --only-changed
πΎ Inventory Management
Static Inventory Example
[web]
10.0.4.10 ansible_user=automation ansible_ssh_private_key_file=~/.ssh/id_rsa
[db]
10.0.4.20
Commands for Inventory
ansible-inventory --list
ansible all --list-hosts
ansible-inventory --graph
π‘ Tip: Terraform can dynamically generate inventory for Ansible using outputs.
π§° Common Ad-Hoc Commands
| Purpose | Command |
|---|---|
| Test connectivity | ansible all -m ping |
| Check uptime | ansible all -a "uptime" |
| Copy files | ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts" |
| Run commands as root | ansible all -b -a "apt update" |
| Gather facts | ansible all -m setup |
| Manage users | ansible all -m user -a "name=test state=present" |
π§Ύ Writing Playbooks
Example Playbook:
---
- name: Initialize new containers
hosts: all
become: yes
tasks:
- name: Update and upgrade system
apt:
update_cache: yes
upgrade: dist
- name: Install connectivity tools
apt:
name:
- traceroute
- curl
- net-tools
state: present
- name: Create user
user:
name: sony
password: "{{ 'password' | password_hash('sha512') }}"
groups: sudo
create_home: yes
Run the playbook:
ansible-playbook playbooks/site.yml
π§© Roles and Reusability
Structure of a role:
roles/
βββ webserver/
βββ tasks/main.yml
βββ handlers/main.yml
βββ vars/main.yml
βββ templates/index.html.j2
βββ meta/main.yml
Initialize a new role:
ansible-galaxy init roles/webserver
Install community roles:
ansible-galaxy install geerlingguy.nginx
ansible-galaxy list
βοΈ Variables and Templates
Define variables:
vars:
app_port: 8080
Template example (nginx.conf.j2):
server {
listen {{ app_port }};
}
Apply template:
ansible all -m template -a "src=nginx.conf.j2 dest=/etc/nginx/sites-available/default"
π§ Tags, Loops, and Conditionals
Loops
- name: Install multiple packages
apt:
name: "{{ item }}"
state: present
loop:
- curl
- vim
- net-tools
Tags
- name: Install NGINX
apt:
name: nginx
state: present
tags: [install, web]
Run specific tags:
ansible-playbook site.yml --tags "install"
Conditionals
- name: Restart service only if Ubuntu
service:
name: nginx
state: restarted
when: ansible_distribution == "Ubuntu"
π§ Handlers and Notifications
tasks:
- name: Update nginx config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify:
- Restart nginx
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
π§Ύ Ansible Vault (Secrets Management)
Encrypt sensitive files:
ansible-vault create secrets.yml
ansible-vault encrypt playbook.yml
ansible-vault decrypt playbook.yml
Run with password prompt:
ansible-playbook play.yml --ask-vault-pass
π‘ Use
--vault-password-file ~/.vault_pass.txtfor automation.
π§© Facts and Filters
Collect system facts:
ansible all -m setup
Filter facts:
ansible all -m setup -a "filter=ansible_distribution"
Example Jinja2 filter:
{{ ansible_hostname | upper }}
βοΈ Debugging & Troubleshooting
| Command | Description |
|---|---|
ansible-playbook -vvv play.yml |
Verbose mode |
ansible-playbook --step play.yml |
Step-by-step run |
ansible-playbook --check play.yml |
Dry-run mode |
ansible -m ping --limit web |
Target limited hosts |
ANSIBLE_KEEP_REMOTE_FILES=1 ansible-playbook play.yml |
Keep temp debug files |
π‘ Add
--diffto preview file changes before applying.
π§Ύ Ansible Command Cheat Sheet
| Category | Command | Description |
|---|---|---|
| Ping | ansible all -m ping |
Test connectivity |
| Run Playbook | ansible-playbook site.yml |
Execute playbook |
| List Hosts | ansible all --list-hosts |
Show hosts |
| Facts | ansible all -m setup |
Gather facts |
| Vault | ansible-vault encrypt file.yml |
Encrypt sensitive files |
| Lint | ansible-lint play.yml |
Syntax check |
| Check Mode | ansible-playbook play.yml --check |
Dry run |
| Tags | ansible-playbook site.yml --tags install |
Filter tasks |
| Limit | ansible-playbook site.yml --limit web |
Run specific hosts |
β Best Practices
- Keep a single inventory per environment (e.g., dev, prod).
- Store configurations in Git.
- Separate secrets into Vaults.
- Validate YAML with
ansible-lint. - Avoid hardcoded paths β use variables.
- Test using
--checkmode before production.
π§ Conclusion
βAnsible is the configuration engine that turns your servers into ready systems.β
Combined with Terraform and Proxmox, it completes your end-to-end automation cycle:
Terraform provisions β Ansible configures β Your environment runs seamlessly.
Use this as a reference, a command companion, and a foundation for scaling your automation environment.
End of Guide β Ansible Complete Reference