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_files plugin
- vars plugin
- roles vars
- /etc/ansible/hosts or your own inventory file
- lookup vars plugin
- register parameter and debug module
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
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.