Ansible
Agentless automation, playbooks, inventory management, roles, and ad-hoc commands

๐น Introduction to Ansible
โ What is Ansible?
Ansible is an open-source automation engine by Red Hat used for:
- โ Configuration Management (e.g., configuring operating systems, installing & managing software packages)
- โ Application Deployment (e.g., zero-downtime rolling updates across web clusters)
- โ Orchestration (e.g., coordinating multi-tier applications like load balancers + app + database)
- โ Provisioning (e.g., provisioning cloud infrastructure on AWS, Azure, GCP, or bare metal)
โ Why Use Ansible?
- ๐น Agentless: No daemons or agents to install on managed nodes (uses native SSH on Linux/Unix and WinRM on Windows)
- ๐น Idempotent: Running tasks repeatedly guarantees the desired state without unwanted duplicate changes
- ๐น Human-Readable: Written in clear, declarative YAML syntax
- ๐น Extensible: Massive ecosystem of pre-built modules and collections on Ansible Galaxy
- ๐น Cross-Platform: Supports Linux, Windows, macOS, networking appliances (Cisco, Juniper, Arista), and cloud platforms
๐ ๏ธ 1. Installing & Setting Up Ansible
โ Installing Ansible on Control Node
# Ubuntu/Debian
sudo apt update
sudo apt install -y ansible
# CentOS/RHEL 8/9
sudo dnf install -y epel-release
sudo dnf install -y ansible
# Using Python pip (platform independent)
python3 -m pip install --user ansible ansible-lintโ Checking Installation & Environment
ansible --versionโ
Ansible Configuration (ansible.cfg)
Ansible searches for ansible.cfg in the following precedence order:
ANSIBLE_CONFIG(environment variable)./ansible.cfg(current working directory)~/.ansible.cfg(user home directory)/etc/ansible/ansible.cfg(global fallback)
Example project ./ansible.cfg:
[defaults]
inventory = ./inventory.ini
remote_user = ubuntu
private_key_file = ~/.ssh/id_rsa
host_key_checking = False
forks = 10
retry_files_enabled = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[ssh_connection]
pipelining = True๐ 2. Managing Inventories (INI & YAML Formats)
โ
INI Format (inventory.ini)
[webservers]
web1 ansible_host=192.168.1.10 ansible_user=ubuntu
web2 ansible_host=192.168.1.11 ansible_user=ubuntu
[dbservers]
db1 ansible_host=192.168.1.20 ansible_user=ubuntu
[production:children]
webservers
dbservers
[production:vars]
ansible_python_interpreter=/usr/bin/python3
env=productionโ
YAML Format (inventory.yml)
all:
children:
webservers:
hosts:
web1:
ansible_host: 192.168.1.10
web2:
ansible_host: 192.168.1.11
vars:
http_port: 80
dbservers:
hosts:
db1:
ansible_host: 192.168.1.20
vars:
db_port: 5432
vars:
ansible_user: ubuntu
env: productionโ
Testing Connectivity with ping
# Ping all managed hosts
ansible all -i inventory.ini -m ping
# Ping only webservers
ansible webservers -m ping๐ 3. Running Ad-Hoc Commands
Ad-hoc commands are ideal for quick one-off administrative tasks. Modern Ansible encourages using Fully Qualified Collection Names (FQCN) like ansible.builtin.<module>.
# Check disk usage on all nodes
ansible all -m ansible.builtin.command -a "df -h"
# Check system uptime
ansible all -m ansible.builtin.command -a "uptime"
# Run command with shell pipes/redirection
ansible all -m ansible.builtin.shell -a "grep -i error /var/log/syslog | tail -n 20"
# Create a directory on remote hosts
ansible all -m ansible.builtin.file -a "path=/opt/app state=directory mode=0755" --become
# Copy file to remote servers
ansible all -m ansible.builtin.copy -a "src=./app.conf dest=/etc/app.conf mode=0644" --become
# Install a package (e.g., nginx) on webservers
ansible webservers -m ansible.builtin.apt -a "name=nginx state=present update_cache=yes" --become
# Restart a service
ansible webservers -m ansible.builtin.service -a "name=nginx state=restarted" --become
# Gather and filter system facts
ansible all -m ansible.builtin.setup -a "filter=ansible_distribution*"๐ 4. Writing Ansible Playbooks
โ Basic Playbook Structure with Handlers
Handlers run only once at the end of a play, and only when a task reporting a change triggers them via notify.
---
- name: Configure Web Server with Nginx
hosts: webservers
become: true
vars:
server_name: example.com
document_root: /var/www/html
tasks:
- name: Ensure Nginx is installed
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Deploy custom index page from template
ansible.builtin.template:
src: templates/index.html.j2
dest: "{{ document_root }}/index.html"
owner: www-data
group: www-data
mode: '0644'
notify: Restart Nginx
- name: Ensure Nginx service is running and enabled
ansible.builtin.service:
name: nginx
state: started
enabled: true
handlers:
- name: Restart Nginx
ansible.builtin.service:
name: nginx
state: restarted๐งฉ 5. Jinja2 Templates (template Module)
Templates let you generate dynamic configuration files with embedded variables, conditions, and loops.
Example template: templates/index.html.j2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to {{ server_name }}</title>
</head>
<body>
<h1>Managed by Ansible</h1>
<p>Server Hostname: {{ ansible_hostname }}</p>
<p>IP Address: {{ ansible_default_ipv4.address }}</p>
<p>Operating System: {{ ansible_distribution }} {{ ansible_distribution_version }}</p>
{% if env == 'production' %}
<p><strong>Environment: PRODUCTION</strong></p>
{% else %}
<p>Environment: {{ env | default('development') }}</p>
{% endif %}
</body>
</html>๐น 6. Loops, Conditionals & Registers
โ
Loop Example (loop)
- name: Install multiple packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- nginx
- git
- curl
- htop
- ufwโ
Register & Conditional Execution (when)
- name: Check if application configuration exists
ansible.builtin.stat:
path: /etc/myapp/config.json
register: config_file
- name: Initialize configuration if missing
ansible.builtin.template:
src: templates/config.json.j2
dest: /etc/myapp/config.json
mode: '0600'
when: not config_file.stat.exists๐ 7. Ansible Roles (Organizing Complex Projects)
Roles allow you to automatically load related vars, files, tasks, handlers, and templates based on a known directory structure.
โ Generate Role Skeleton
ansible-galaxy role init roles/nginxDirectory Structure:
roles/nginx/
โโโ defaults/ # Default variables (lowest precedence)
โ โโโ main.yml
โโโ vars/ # Role variables (high precedence)
โ โโโ main.yml
โโโ tasks/ # Main list of tasks executed by the role
โ โโโ main.yml
โโโ handlers/ # Handlers triggered by notify
โ โโโ main.yml
โโโ templates/ # Jinja2 templates (.j2)
โ โโโ nginx.conf.j2
โโโ files/ # Static files copied to target machines
โโโ meta/ # Role dependencies and author info
โ โโโ main.yml
โโโ README.mdโ Using Roles in a Playbook
---
- name: Deploy Production Application Stack
hosts: webservers
become: true
roles:
- role: common
- role: nginx
vars:
nginx_port: 8080
- role: myapp๐ 8. Ansible Vault (Encrypting Sensitive Data)
Ansible Vault encrypts sensitive variables, passwords, SSH keys, or entire files.
# Create a new encrypted file
ansible-vault create vars/secrets.yml
# Edit an existing encrypted file
ansible-vault edit vars/secrets.yml
# Encrypt an existing plaintext file
ansible-vault encrypt credentials.txt
# Decrypt an encrypted file
ansible-vault decrypt credentials.txt
# View encrypted file contents without editing
ansible-vault view vars/secrets.yml
# Re-key with a new password
ansible-vault rekey vars/secrets.ymlโ Running Playbooks with Vault
# Prompt for password interactively
ansible-playbook site.yml --ask-vault-pass
# Read password from a secure file
ansible-playbook site.yml --vault-password-file ~/.vault_pass.txt๐ฏ 9. Playbook Execution Flags & Workflow Control
| Command / Flag | Purpose |
|---|---|
ansible-playbook site.yml --syntax-check | Check playbook for syntax errors without running |
ansible-playbook site.yml --check | Dry run mode: simulate changes without modifying systems |
ansible-playbook site.yml --check --diff | Simulate changes and show a unified diff of file alterations |
ansible-playbook site.yml --limit web1 | Limit execution to specific host(s) or group (-l) |
ansible-playbook site.yml --tags "deploy" | Run only tasks tagged with deploy (-t) |
ansible-playbook site.yml --skip-tags "cleanup" | Skip tasks tagged with cleanup |
ansible-playbook site.yml --step | Interactive execution: confirm each task one by one |
ansible-playbook site.yml --start-at-task "Start Service" | Resume playbook execution from a specific task |
ansible-playbook site.yml -e "version=2.1.0" | Pass extra runtime variables (--extra-vars) |
๐ฆ 10. Ansible Galaxy Collections
Ansible modularized features into collections (namespace.collection).
# Install a collection from Galaxy
ansible-galaxy collection install amazon.aws
ansible-galaxy collection install community.docker
ansible-galaxy collection install kubernetes.core
# Install all dependencies from a requirements.yml file
ansible-galaxy install -r requirements.ymlExample requirements.yml:
---
roles:
- name: geerlingguy.docker
version: 7.4.4
collections:
- name: amazon.aws
version: 7.3.0๐ง Tips & Production Best Practices
- Always Use FQCN: Specify
ansible.builtin.copyrather than short namecopyto avoid namespace collisions. - Lint Every Commit: Use
ansible-lintin CI pipelines to catch anti-patterns and deprecations. - Use Handlers for Restarts: Avoid restarting services inside tasks; notify handlers so services only restart when configuration files change.
- Never Store Secrets in Plaintext: Use
ansible-vaultfor tokens, passwords, and private keys. - Always Set File Permissions: Always declare
mode: '0644'ormode: '0755'when creating files and directories. - Set
ansible_managedHeader: Add{{ ansible_managed }}at the top of templates to warn users against manual edits.
๐ Learning Resources
- ๐ Official Ansible Documentation
- ๐ฆ Ansible Galaxy
- ๐ Ansible Module Index
- ๐งช Ansible Lint Guide