Ansible - Getting Started with variables
by
Jeremy Canfield |
Updated: February 16 2023
| Ansible articles
Following are the differet ways that variables can be set in Ansible. This list is in the order of precedence, where the option higher in the list takes precedence over options lower in the list.
- --extra-vars command line option or Extra Variables in Tower
- set_fact module
- include_vars module
- vars_prompt
- vars_files plugin
- vars plugin
- roles vars
- group_vars
- /etc/ansible/hosts or your own inventory file
- lookup vars plugin
- register parameter and debug module
Precedence
Let's say you have a variable defined twice. For example, let's say the foo variable is defined in the vars plugin and contains a value of Hello.
---
- hosts: all
vars:
foo: "Hello"
Let's also say that the --extra-vars command line option is being used to set the foo variable to contain a value of World.
ansible-playbook example.yml --extra-vars "foo=World"
In this scenario, the foo variable would contain a value of World, because the --extra-vars command line option takes precedence over the vars plugin.
Did you find this article helpful?
If so, consider buying me a coffee over at