Quiz Entry - updated: 2026.11.05
How do you define variables directly in a play, and how do you load them from a separate file instead?
Put them in a vars: block at the start of the play, or list external YAML files under vars_files: (paths relative to the playbook). Either way they become play variables, usable in every task of that play.
- hosts: all
vars:
user: joe
home: /home/joe
tasks: ...
- hosts: all
vars_files:
- vars/users.yml
and vars/users.yml contains plain key-value pairs, with no play structure around them:
user: joe
home: /home/joe
Why move variables out into a file? The playbook stays short and readable, the same variable file can be shared between playbooks, and, most importantly, a file can be encrypted with Ansible Vault while the playbook stays readable. That is the usual way to keep passwords out of plain-text playbooks.
Go deeper:
Defining variables in included files and roles (official docs) โ
vars,vars_filesand where each fits.