Ansible: Playbooks, Roles, Inventory and Vault Reference Guide
By DevShelfHub
Inventory, modules, playbooks, roles, variables, templates, vault, handlers — the day-to-day Ansible surface for ansible-core 2.17+. Covers declarative YAML automation from ad-hoc commands through rolling deployments with full-qualified collection names (FQCN).
96 items
◷ 8 min
Playbooks
Roles
Inventory
Start hereQuick start · 6 you’ll reach for daily
Pingansible all -m ping
Run playansible-playbook site.yml
Dry run--check --diff
Limit--limit web --tags db
Vaultansible-vault encrypt …
Becomebecome: true
Target versions · paceVersions
Targets:ansible-core ≥ 2.17python ≥ 3.10 (control)collections from galaxy.ansible.com
Snippets use modern fully-qualified collection names (FQCN): e.g.
ansible.builtin.apt, not just apt.
The shortform still works but FQCN ages better and is required for non-builtin modules. Install community
bundle separately: pipx inject ansible-core ansible.
install · ad-hoc · vaultSetup
bash
# Install
pipx install ansible-core # core engine
pipx inject ansible-core ansible # add the community collection bundle
ansible --version
# Quick connectivity check
ansible -i inventory.ini all -m ping
# Run an ad-hoc command
ansible -i inventory.ini web -m shell -b -a "uptime"
# Run a playbook
ansible-playbook -i inventory.ini site.yml --check # dry-run
ansible-playbook -i inventory.ini site.yml --diff # show file diffs
ansible-playbook -i inventory.ini site.yml --limit web # subset of hosts
ansible-playbook -i inventory.ini site.yml --tags db # subset of tasks
# Vault
ansible-vault create secrets.yml
ansible-vault edit secrets.yml
ansible-vault encrypt vars/prod.yml
ansible-playbook ... --ask-vault-pass
Use fully-qualified collection names.ansible.builtin.apt, not bare apt. The short
form still works for builtin modules but breaks the moment you add a custom collection with a
same-named module.
--check --diff first, always.
Dry-run gives you a textual diff of the changes Ansible would make. Catches accidental
production typos at zero cost.
Idempotent > clever.
A second run should make zero changes. If command / shell
is your hammer, the play isn’t idempotent. Reach for native modules + creates /
removes hints.
Common trapsWatch out for
Variable precedence is non-obvious.--extra-vars beats everything; role vars/
beats play vars; role defaults/ sits at the bottom. When “my variable
isn’t taking effect”, walk the precedence list.
command with shell features fails silently.cat file | grep x through command treats the
pipe as an argument to cat. Use shell — and
know you’ve given up portability.
Handlers don’t run if the play aborts.
A failure before flush_handlers means nginx never reloads. Set
force_handlers: true when correctness matters more than failure speed.
Ansible is an agentless IT automation tool that uses SSH and YAML playbooks to configure servers, deploy applications, and orchestrate infrastructure. It is idempotent — running the same playbook twice leaves the system in the same state.
How do I run an Ansible playbook?
Run ansible-playbook site.yml -i inventory.ini. Add --check --diff for a dry-run that shows what would change without applying it. Use --limit web to target a single group and --tags db to run only tagged tasks.
What is an Ansible role?
A role is a reusable, self-contained unit of automation with a standard directory layout: tasks/, handlers/, defaults/, vars/, templates/, and files/. Roles are referenced in playbooks with roles: [my_role] and can be shared via Ansible Galaxy.
How does Ansible Vault work?
Ansible Vault encrypts sensitive files or individual strings using AES-256. Encrypt with ansible-vault encrypt secrets.yml and decrypt at runtime with --ask-vault-pass or a vault password file. Inline encrypted values use the !vault YAML tag.
Does Ansible need an agent installed on managed hosts?
No. Ansible is agentless. It connects to managed hosts over SSH (or WinRM for Windows) using standard credentials. The control node needs Python and the ansible-core package; managed hosts only need Python and an SSH server.
What is the difference between ansible and ansible-playbook?
ansible runs a single ad-hoc module against hosts (e.g. ansible all -m ping). ansible-playbook runs a full YAML playbook with multiple plays and tasks. Use ad-hoc commands for one-off checks and playbooks for repeatable automation.