Ansible Automation Guide
Automate configuration management, application deployment, and IT orchestration with Ansible — agentless, YAML-based, and works over plain SSH with no agents required.
What is Ansible?
Ansible is an open-source IT automation tool created in 2012 and maintained by Red Hat. It automates configuration management, application deployment, cloud provisioning, and orchestration across Linux, Windows, network devices, and cloud platforms. Unlike agent-based tools, Ansible requires no software on managed nodes — it connects over SSH, pushes small Python modules, executes them, and cleans up. Everything is described in human-readable YAML files called playbooks.
Ansible's simplicity is its greatest strength. A sysadmin who knows SSH and YAML can write their first working playbook within minutes. Ansible is idempotent by design — running a playbook multiple times produces the same result, making it safe to apply repeatedly. It is used by organizations from single-server projects to global enterprises managing hundreds of thousands of nodes with Ansible Automation Platform (AWX/Tower).
Key Features
- Agentless architecture — SSH and Python only, no agent to install or maintain on managed nodes
- YAML playbooks — human-readable, version-controlled, self-documenting automation code
- Roles — reusable, shareable task collections with standard directory layout (tasks/, handlers/, templates/, vars/)
- Dynamic inventory — pull host lists from AWS EC2, GCP, Azure, or any API at runtime
- Handlers — event-driven tasks that fire once at end of play when notified by changed tasks
- Ansible Vault — encrypt secrets at rest, decrypt transparently at runtime
- Jinja2 templates — dynamically generate config files with variables and filters
- AWX / Automation Platform — web UI, RBAC, scheduling, and REST API on top of Ansible core
Inventory File
# inventory/hosts.ini [web] web1.example.com ansible_user=ubuntu web2.example.com ansible_user=ubuntu [db] db1.example.com ansible_user=postgres ansible_port=2222 [production:children] web db [production:vars] ansible_ssh_private_key_file=~/.ssh/prod_key env=production
Example Playbook
# site.yml
- name: Configure web servers
hosts: web
become: true
vars:
nginx_port: 80
app_user: www-data
pre_tasks:
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
- name: Deploy nginx configuration
ansible.builtin.template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: '0644'
notify: Restart nginx
- name: Ensure nginx is enabled and started
ansible.builtin.systemd:
name: nginx
state: started
enabled: true
- name: Deploy application code
ansible.builtin.git:
repo: https://github.com/example/app.git
dest: /var/www/app
version: "{{ app_version | default('main') }}"
notify: Restart app service
handlers:
- name: Restart nginx
ansible.builtin.systemd:
name: nginx
state: restarted
- name: Restart app service
ansible.builtin.systemd:
name: myapp
state: restarted Jinja2 Template Example
{# templates/nginx.conf.j2 #}
worker_processes {{ ansible_processor_vcpus }};
events {
worker_connections 1024;
}
http {
server {
listen {{ nginx_port }};
server_name {{ inventory_hostname }};
root /var/www/{{ app_name | default('app') }};
{% if ssl_enabled | default(false) %}
listen 443 ssl;
ssl_certificate /etc/ssl/certs/{{ inventory_hostname }}.crt;
ssl_certificate_key /etc/ssl/private/{{ inventory_hostname }}.key;
{% endif %}
location / {
proxy_pass http://127.0.0.1:{{ app_port | default(3000) }};
proxy_set_header Host $host;
}
}
} Ansible vs Configuration Management Alternatives
| Feature | Ansible | Chef | Puppet | SaltStack |
|---|---|---|---|---|
| Architecture | Agentless (SSH) | Agent (Chef Client) | Agent (Puppet Agent) | Agent (Minion) or agentless |
| Language | YAML | Ruby (DSL) | Puppet DSL / Ruby | YAML / Python |
| Learning curve | Low | High | Medium-High | Medium |
| Push vs Pull | Push | Pull | Pull | Push or Pull |
| Windows support | Good (WinRM) | Good | Good | Limited |
| Community | Very large (Galaxy) | Medium (Supermarket) | Medium (Forge) | Medium |
Use Cases
- Server provisioning and baseline configuration (packages, users, SSH keys, firewall rules)
- Application deployment pipelines with zero-downtime rolling updates
- Network device automation (Cisco IOS, Juniper, Arista via network modules)
- Cloud provisioning and multi-tier application orchestration across AWS/GCP/Azure
- Security hardening and compliance enforcement via CIS benchmark playbooks
- Disaster recovery automation — fully reproducible environment restoration from playbooks
Pros and Cons
Pros
- ✓ No agent installation required — works on any SSH-accessible host
- ✓ Very low barrier to entry: YAML + SSH knowledge is enough
- ✓ Huge community and Galaxy collection ecosystem
- ✓ Excellent for ad-hoc commands and one-off tasks
- ✓ Idempotent by design — safe to run repeatedly
- ✓ Vault provides built-in secret management
Cons
- ✗ Push model requires control node reachability to all hosts
- ✗ SSH overhead — slower than agent-based tools at scale (1000+ nodes)
- ✗ YAML indentation errors can be frustrating to debug
- ✗ No built-in state tracking — re-runs re-execute all tasks
- ✗ Complex conditional logic becomes unwieldy in YAML
- ✗ AWX/Tower required for enterprise scheduling and RBAC (additional infra)
Frequently Asked Questions
What makes Ansible 'agentless' and why does it matter?▼
Ansible connects to managed nodes over SSH (Linux) or WinRM (Windows) without requiring any software to be pre-installed on those hosts. It copies small Python scripts to the remote node, executes them, then removes them. This agentless architecture dramatically simplifies adoption — you don't need to bootstrap an agent on every server before you can manage it. It also reduces the attack surface and eliminates the need to maintain and update agents across your fleet. The only requirement is Python on the managed node and SSH access from the control node.
What is the difference between a task, role, and playbook in Ansible?▼
A task is a single unit of work — one call to an Ansible module (e.g., install a package, copy a file, restart a service). A playbook is a YAML file that defines a sequence of plays; each play targets a group of hosts and runs a list of tasks against them. A role is a reusable, self-contained unit that packages tasks, variables, handlers, templates, and files into a structured directory layout. Roles enable code reuse across playbooks and projects, similar to a library or module in programming.
What are Ansible handlers and when should I use them?▼
Handlers are special tasks that run only when notified by another task that reports 'changed'. They execute once at the end of a play, regardless of how many tasks notified them. The canonical use case is restarting a service after its configuration file changes: the task that copies the config file notifies the 'restart nginx' handler. Without handlers you'd either always restart the service (wasteful) or write complex conditional logic. Handlers run in the order they are defined, not the order they are notified.
How do I manage secrets in Ansible?▼
Use Ansible Vault to encrypt sensitive data at rest. ansible-vault encrypt_string encrypts an individual string for use inline in a vars file. ansible-vault encrypt secrets.yml encrypts an entire file. Encrypted values are decrypted at runtime using a vault password (provided via --vault-password-file, ANSIBLE_VAULT_PASSWORD_FILE env var, or prompted interactively). For production, integrate Vault passwords with a secrets manager (HashiCorp Vault, AWS Secrets Manager) using a custom vault password script rather than storing the vault password on disk.
What is Ansible Galaxy and how do I use community roles?▼
Ansible Galaxy is the official hub for sharing Ansible roles and collections. A collection bundles related roles, modules, plugins, and documentation into a single package. Install a role or collection with ansible-galaxy install geerlingguy.nginx or ansible-galaxy collection install community.docker. Define project dependencies in requirements.yml and install them all with ansible-galaxy install -r requirements.yml. This lets you leverage community-maintained roles for common tasks (LAMP stack, Docker, Kubernetes) rather than writing everything from scratch.
What is the difference between AWX and Ansible Tower?▼
AWX is the open-source, community-supported upstream project for Red Hat Ansible Automation Platform (formerly Ansible Tower). AWX provides a web UI, REST API, role-based access control (RBAC), job scheduling, credential management, and inventory management on top of Ansible. Tower is the supported, enterprise-licensed version with SLA and Red Hat integration. For teams without an enterprise license, AWX deployed on Kubernetes via its operator is the recommended path.
Related Products
on GumroadGitHub Actions CI/CD Templates Pack
12 production-ready GitHub Actions workflow templates covering Node.js, Python, Docker, multi-environment deployments, security scanning, and auto-changelog. Copy, paste, ship.
Buy on Gumroad →Premium Workflow Pack
Production-grade workflows, architecture guides, and deployment scripts for teams that ship. 10 advanced CI/CD workflows (blue-green, canary, E2E, security), 5 architecture deep-dives, code review checklist, PR process guide, and 5 battle-tested Bash deployment scripts.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
Ansible Playbook Builder
Build Ansible playbooks visually with a task composer. Add apt, yum, copy, service, command, git, and other module tasks. Generate valid YAML playbooks.
Open free tool →
Ansible Playbook Linter
Paste Ansible YAML playbooks and check for common issues, best practices, and syntax errors. Validates task structure, module usage, and playbook organization.
Open free tool →
Pulumi Modern IaC Guide
Complete Pulumi infrastructure as code guide. Learn TypeScript/Python programs, state management, stacks, Automation API, CrossGuard policies, and comparison with Terraform and CDK.
Open free tool →
More Free Tools
Related Articles
Ansible vs Chef vs Puppet: Configuration Management Tools Compared 2026
Ansible vs Chef vs Puppet — compare agentless vs agent-based architecture, YAML vs Ruby DSL, scalability, Windows support, and cloud integration. Find the right config management tool for 2026.
DevOps Automation Tools in 2026: Orchestrating the Software Delivery Pipeline
Master DevOps automation with this guide to the tools that orchestrate modern software delivery. Covers CI/CD pipelines, infrastructure provisioning, configuration management, testing automation, and the emerging AI-assisted automation patterns that define 2026.
GitHub Actions vs GitLab CI 2026: Complete CI/CD Platform Comparison
Detailed GitHub Actions vs GitLab CI/CD comparison for 2026 — pricing, runners, YAML syntax, secrets, artifacts, caching, and migration guide.
Dagger vs GitHub Actions vs Jenkins: Modern CI/CD Pipeline Comparison 2026
Compare Dagger, GitHub Actions, and Jenkins for CI/CD in 2026. Real-world benchmarks, architecture differences, pricing, and a recommendation matrix to pick the right tool.
Docker vs Kubernetes: When to Use Each (2025 Guide)
Docker vs Kubernetes 2025: key differences, Docker Compose vs Kubernetes, cost comparison, and a clear decision framework with real-world use cases.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →