Skip to main content

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.

✓ Agentless (SSH / WinRM) ✓ Idempotent playbooks ✓ Ansible Vault secrets ✓ Galaxy collections

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

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

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.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →