AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible FAQ — Frequently Asked Questions

Answers to the most common Ansible questions: installation, playbooks, roles, modules, troubleshooting, and best practices. 30 curated questions across 10 categories.

What is Ansible and why should I use it?
Ansible is an open-source IT automation tool that enables infrastructure as code, configuration management, and application deployment. It uses a simple, human-readable YAML syntax called playbooks and doesn't require agents on managed nodes (agentless). You should use Ansible because it reduces manual configuration errors, ensures consistency across environments, and significantly speeds up deployment processes.
How do I install Ansible?
Ansible can be installed on most Unix-like systems including Linux and macOS. On RHEL/CentOS, use 'sudo yum install ansible' or 'sudo dnf install ansible'. On Ubuntu/Debian, use 'sudo apt update && sudo apt install ansible'. On macOS, use 'brew install ansible'. For the latest version, you can also install via pip: 'pip install ansible'. Windows can be a control node using WSL (Windows Subsystem for Linux).
What is the difference between Ansible and Ansible Tower/AWX?
Ansible is the core automation engine that runs playbooks from the command line. Ansible Tower (now called Ansible Automation Platform) and AWX (the open-source upstream project) provide a web-based UI, REST API, role-based access control, job scheduling, and centralized logging. Tower/AWX is ideal for enterprise environments where multiple teams need to collaborate and audit automation workflows.
Do I need to install agents on managed nodes?
No, Ansible is agentless. It connects to managed nodes using SSH (for Linux/Unix) or WinRM (for Windows) and executes tasks remotely. This makes it easier to get started since there's nothing to install or maintain on target systems. Ansible only requires Python on the managed nodes, which is typically already installed on most systems.
What is an Ansible playbook?
An Ansible playbook is a YAML file that defines a set of tasks to be executed on managed hosts. Playbooks describe the desired state of your infrastructure and contain plays (which target specific hosts) and tasks (which call Ansible modules). They are the primary way to orchestrate multi-machine deployments and define complex automation workflows.
How do I debug a failing playbook?
There are several ways to debug Ansible playbooks: 1) Use '-v' flags for verbosity (up to -vvvv for maximum detail). 2) Add 'debug' tasks to print variable values. 3) Use '--step' to execute one task at a time. 4) Use '--start-at-task' to resume from a specific task. 5) Check the registered variables with 'register' and 'debug'. 6) Use '--check' mode for dry runs. 7) Enable ANSIBLE_DEBUG=1 environment variable for internal debugging.
What's the difference between 'handlers' and 'tasks'?
Tasks are executed sequentially in order as they appear in the playbook. Handlers are special tasks that only run when notified by other tasks and only run once at the end of a play, regardless of how many times they're notified. Handlers are typically used for actions like restarting services after configuration changes, ensuring the service only restarts once even if multiple configuration files changed.
How do I use variables in Ansible?
Variables can be defined in many places: 1) In playbooks using 'vars' or 'vars_files'. 2) In inventory files or group_vars/host_vars directories. 3) Passed on command line with '-e'. 4) Registered from task output using 'register'. 5) Gathered automatically as 'facts' from hosts. 6) In role defaults or vars. Variables are referenced using Jinja2 syntax: {{ variable_name }}. Ansible has a specific variable precedence order, with command-line variables having the highest priority.
What is an Ansible inventory?
An inventory is a file or directory that defines the hosts and groups of hosts Ansible will manage. The default location is /etc/ansible/hosts, but you can specify any file with '-i'. Inventories can be static (INI or YAML format) or dynamic (scripts or plugins that query external sources like AWS, Azure, or VMware). Inventories can also define host-specific and group-specific variables.
How do I organize hosts into groups?
In INI format, create groups using [groupname] headers. In YAML, use 'all: children: groupname: hosts:' structure. You can nest groups using [groupname:children]. Common patterns include grouping by function (webservers, databases), environment (production, staging), or location (datacenter1, datacenter2). The 'all' group contains all hosts, and 'ungrouped' contains hosts not in any other group.
What are dynamic inventories and when should I use them?
Dynamic inventories are scripts or plugins that generate inventory data from external sources at runtime. Use them when: your infrastructure changes frequently, you use cloud providers (AWS, Azure, GCP), you have a CMDB or asset management system, or you want a single source of truth. Ansible includes many inventory plugins for common platforms. Dynamic inventories are especially valuable in auto-scaling environments.
What are Ansible modules?
Modules are the units of work in Ansible - discrete pieces of code that perform specific tasks like managing files, installing packages, or configuring services. Ansible includes thousands of built-in modules covering cloud services, databases, networking, and more. Modules are idempotent by design, meaning running them multiple times produces the same result. You can also write custom modules in Python.
What are Ansible Collections?
Collections are a distribution format for Ansible content including modules, plugins, roles, and playbooks. Introduced in Ansible 2.9, they allow content to be developed and distributed independently from Ansible core. Collections are installed from Ansible Galaxy using 'ansible-galaxy collection install'. The namespace format is 'namespace.collection_name' (e.g., 'ansible.posix', 'community.general').
How do I find the right module for my task?
1) Use 'ansible-doc -l' to list all available modules. 2) Use 'ansible-doc module_name' for detailed documentation. 3) Search Ansible Galaxy (galaxy.ansible.com) for collections. 4) Check the official Ansible documentation at docs.ansible.com. 5) The module index is categorized by function (cloud, database, files, etc.). Most common tasks have dedicated modules - avoid using 'shell' or 'command' when a specific module exists.
What are Ansible roles and why use them?
Roles are a way to organize playbooks into reusable components with a standardized directory structure. A role includes tasks, handlers, variables, templates, and files in predefined directories. Use roles to: share automation across projects, enforce consistent patterns, separate concerns, and collaborate with others. Roles can be shared via Ansible Galaxy and version-controlled independently.
How do I create an Ansible role?
Use 'ansible-galaxy role init role_name' to create a role skeleton. This creates a directory structure with: tasks/main.yml (entry point), handlers/, templates/, files/, vars/, defaults/, and meta/. Place your tasks in tasks/main.yml, default variables in defaults/main.yml, and dependencies in meta/main.yml. Then include the role in your playbook using the 'roles:' keyword or 'include_role'/'import_role'.
What's the difference between import and include?
'import_*' (import_tasks, import_role) is static - processed at playbook parsing time, before execution begins. 'include_*' (include_tasks, include_role) is dynamic - processed during playbook execution. Use import when you want consistent behavior and include when you need conditional includes or loops. Imports support tags fully, while includes have some tag limitations.
How do I store secrets securely in Ansible?
Use Ansible Vault to encrypt sensitive data. Create encrypted files with 'ansible-vault create secrets.yml' or encrypt existing files with 'ansible-vault encrypt file.yml'. Run playbooks with '--ask-vault-pass' or '--vault-password-file'. You can encrypt entire files or just specific variables using 'ansible-vault encrypt_string'. Never commit unencrypted secrets to version control.
What are Ansible best practices for production?
Key best practices include: 1) Use version control for all Ansible code. 2) Organize with roles and collections. 3) Use group_vars and host_vars for variables. 4) Encrypt secrets with Ansible Vault. 5) Test with '--check' and '--diff' modes. 6) Use meaningful names and comments. 7) Keep playbooks idempotent. 8) Use tags for selective execution. 9) Implement proper error handling. 10) Maintain a consistent directory structure.
How do I handle errors in Ansible?
Ansible provides several error handling mechanisms: 'ignore_errors: yes' continues on failure, 'failed_when' defines custom failure conditions, 'block/rescue/always' provides try-catch-finally semantics, 'any_errors_fatal: true' stops all hosts on any failure. Use 'register' to capture results and 'assert' to validate conditions. The 'meta: clear_host_errors' can reset error state for hosts.
How can I make Ansible run faster?
Optimization strategies include: 1) Increase 'forks' in ansible.cfg (default is 5). 2) Use 'strategy: free' for parallel task execution. 3) Enable pipelining in SSH. 4) Disable 'gather_facts' when not needed or cache facts. 5) Use 'async' for long-running tasks. 6) Optimize your inventory (avoid large groups). 7) Use 'serial' for rolling updates. 8) Consider 'mitogen' strategy for significant speedups.
What is Ansible fact caching?
Fact caching stores gathered host facts between playbook runs, avoiding the overhead of re-gathering facts each time. Enable it in ansible.cfg with 'fact_caching = jsonfile' (or redis, memcached). Set 'fact_caching_connection' to the cache location and 'fact_caching_timeout' for expiration. This can significantly speed up playbook execution, especially with large inventories.
Why is my Ansible playbook running slowly?
Common causes: 1) Too few forks (increase in ansible.cfg). 2) Unnecessary fact gathering (disable or cache). 3) Linear strategy (try 'free' strategy). 4) SSH connection overhead (enable pipelining). 5) Too many small tasks (combine into larger tasks). 6) Synchronous waits (use async). 7) Large file transfers (use synchronize instead of copy). Profile with 'callback_whitelist = profile_tasks'.
How do I fix 'Permission denied' errors?
Check: 1) SSH key configuration ('ansible_ssh_private_key_file'). 2) User has sudo access for 'become' tasks. 3) 'become_method' and 'become_user' are correct. 4) Target file/directory permissions. 5) SELinux contexts on RHEL systems. 6) SSH key permissions (should be 600). Use '-vvvv' to see detailed SSH connection info. Try 'ansible_become_password' if sudo requires password.
Why does 'changed' appear when nothing changed?
Some modules aren't fully idempotent by default. Common causes: 1) 'shell' and 'command' modules always report changed unless you add 'changed_when: false' or check output. 2) File permissions differ from defaults. 3) Template whitespace differences. 4) Package module with '*' for version. Use 'changed_when' to control change detection and '--diff' to see what changed.
Can Ansible manage Windows servers?
Yes! Ansible manages Windows using WinRM (Windows Remote Management) instead of SSH. You need: 1) PowerShell 3.0+ and .NET 4.0+ on targets. 2) WinRM configured and enabled. 3) 'ansible_connection: winrm' in inventory. 4) Windows-specific modules (win_* prefix). Ansible also supports Windows with OpenSSH. The 'ansible.windows' collection provides comprehensive Windows automation.
How do I set up WinRM for Ansible?
On Windows targets: 1) Enable WinRM: 'winrm quickconfig'. 2) Set up authentication (Basic, Certificate, or Kerberos). 3) Configure HTTPS for production. Use the ConfigureRemotingForAnsible.ps1 script for quick setup. In inventory, set: 'ansible_connection: winrm', 'ansible_winrm_transport: ntlm' (or kerberos), and appropriate credentials. Test with 'ansible windows_hosts -m win_ping'.
How do I use Ansible with AWS?
Install the 'amazon.aws' collection: 'ansible-galaxy collection install amazon.aws'. Configure AWS credentials via environment variables, ~/.aws/credentials, or IAM roles. Use modules like 'ec2_instance', 's3_bucket', 'rds_instance', etc. Use dynamic inventory with 'aws_ec2' plugin to automatically discover EC2 instances. The collection supports most AWS services.
Can Ansible manage Kubernetes?
Yes! The 'kubernetes.core' collection provides modules for managing Kubernetes resources. Use 'k8s' module to create/modify resources from YAML definitions, 'k8s_info' to query resources, and 'helm' for Helm chart management. Authentication uses kubeconfig or in-cluster credentials. Ansible can provision clusters, deploy applications, and manage the entire Kubernetes lifecycle.
How do I use Ansible with Docker?
The 'community.docker' collection provides Docker automation. Use 'docker_container' to manage containers, 'docker_image' for images, 'docker_compose' for compose files, and 'docker_network' for networks. You can also use Ansible to build images with 'docker_image' module's 'build' option. Connect to remote Docker hosts via SSH or TCP.