liminfo

Ansible Reference

Free reference guide: Ansible Reference

43 results

About Ansible Reference

The Ansible Reference is a practical, searchable cheat sheet covering the complete Ansible automation framework syntax. It spans eight core categories: Playbook structure (including pre_tasks, post_tasks, tags, and serial rolling updates), Modules (apt, yum, copy, template, service, file, lineinfile, shell, user), Variables (vars, vars_files, register, facts, set_fact, host/group vars), Tasks (when conditions, loop, with_dict, block/rescue, failed_when, delegate_to), Handlers (notify, listen, flush_handlers), Roles (directory structure, dependencies, Ansible Galaxy), Inventory (INI, YAML, dynamic inventory, host patterns), and Vault (encrypt, decrypt, view, vault ID).

This reference is designed for DevOps engineers, SREs, and system administrators who automate infrastructure with Ansible. Whether you are writing your first playbook or fine-tuning a multi-environment role with Galaxy dependencies and Vault-encrypted secrets, this cheat sheet surfaces the exact syntax you need in seconds. Each entry includes a concise description and a copy-ready YAML example drawn from real-world automation tasks.

The content is organized into eight clearly labeled categories so you can either search for a specific keyword like "register" or "serial" or browse the Vault section to recall encrypt_string syntax. The reference covers both Debian/Ubuntu and RHEL/CentOS module variants, making it useful across heterogeneous server fleets. All processing runs in your browser — no sign-up, no server calls, and full dark mode support.

Key Features

  • Playbook fundamentals: basic structure, multiple plays, pre_tasks/post_tasks, tags, and serial rolling updates
  • Core modules: apt, yum, copy, template, service, file, lineinfile, shell/command, and user
  • Variable management: vars, vars_files, register, Ansible facts, set_fact, and host/group_vars
  • Task control: when conditions, loop, with_dict, block/rescue error handling, failed_when, and delegate_to
  • Handler patterns: notify, multiple handlers, listen topic groups, and flush_handlers for immediate execution
  • Role architecture: directory layout, role dependencies in meta/main.yml, and Ansible Galaxy requirements.yml
  • Inventory formats: INI and YAML static inventory, dynamic inventory plugins (e.g. aws_ec2), and host patterns
  • Ansible Vault: file encryption/decryption, encrypt_string, vault view, --vault-password-file, and vault IDs

Frequently Asked Questions

What does this Ansible reference cover?

It covers all eight major areas of Ansible: Playbook structure, built-in Modules (apt, yum, copy, template, service, file, lineinfile, shell, user), Variable handling (vars, register, facts, set_fact), Task control (when, loop, block/rescue), Handlers, Roles and Galaxy, Inventory (INI, YAML, dynamic), and Vault encryption.

How do I run only specific tasks using Ansible tags?

Add a "tags" key to your task with a list of tag names, then run "ansible-playbook site.yml --tags install" to execute only tagged tasks. You can also use "--skip-tags" to exclude specific tags.

What is the difference between the copy and template modules?

The copy module transfers a static file from the controller to the remote host. The template module processes a Jinja2 (.j2) file on the controller, substituting variables before transferring the rendered result to the remote host.

How does "register" work in Ansible?

The "register" keyword captures the full return value of a task (stdout, stderr, rc, changed, etc.) into a named variable. You can then reference that variable in a subsequent "when" condition or "debug" task, for example: "when: deploy_result.rc == 0".

What is "serial" used for in a playbook?

"serial" controls the batch size for rolling updates. Setting "serial: 2" means Ansible processes only 2 hosts at a time instead of all hosts simultaneously, allowing you to keep part of the fleet serving traffic during deployments.

How do I organize secrets in Ansible Vault?

Store sensitive variables in a dedicated file (e.g. vars/secrets.yml) and encrypt it with "ansible-vault encrypt vars/secrets.yml". Reference the file in vars_files. At playbook runtime, pass the vault password via "--ask-vault-pass" or "--vault-password-file ~/.vault_pass". For multi-environment setups, use vault IDs.

What is the purpose of handlers and when are they triggered?

Handlers are tasks that run only when notified by another task using "notify". They execute once at the end of the play (or immediately if flush_handlers is used), making them ideal for service restarts after config changes. Multiple tasks can notify the same handler, and it will only run once.

How do I use a dynamic inventory for AWS EC2?

Create an aws_ec2.yml file with "plugin: aws_ec2", specify your regions, and optionally use keyed_groups to map EC2 tags to Ansible inventory groups. Run your playbook with "-i aws_ec2.yml" to automatically target running EC2 instances without a static inventory file.