Interview Prep/Ansible/Variables, Facts and Precedence

What are the variable scopes in Ansible?

IntermediateConceptVariables, Facts and Precedence
Answer
  • Global: config settings, environment, and -e extra vars
  • Play: vars, vars_files, roles
  • Host: inventory host and group vars, facts, registered variables and set_fact, which stay with a host across plays in the same run
  • Block and task: vars set on them live only inside

Registered and set_fact values persist for the host for the rest of the run, including later plays. Task vars disappear after the task.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence