While troubleshooting an unexpected configuration change, you discover that the variable app_version is defined in four different places. The playbook is executed with:
ansible-playbook site.yml -e "app_version=3.4.9"
No other definitions of the variable exist. The same variable is also set in group_vars/webservers.yml, inside host-specific variables in the inventory file, and in the role's defaults/main.yml. Which definition will Ansible apply during the run?
The value set for the host in the inventory file.
The default value in the role's defaults/main.yml file.
The value defined in group_vars/webservers.yml.
The value supplied with --extra-vars at runtime (3.4.9).
Ansible resolves variable conflicts according to its precedence rules. Values supplied with the --extra-vars (or -e) option are always at the top of the hierarchy and override any other source, including host-specific variables, group variables, and role defaults. Group and host variables defined in inventory or playbook directories take precedence over role defaults, but they are still ignored when an extra variable of the same name is provided on the command line. Therefore, the value passed with -e "app_version=3.4.9" is the one that Ansible will use, and all other definitions are ignored for that run.
Host variables, group variables, and role defaults are valid ways to set persistent values, but they have progressively lower priority. Role defaults in defaults/main.yml sit at the very bottom of the list and can be overridden by almost any other method.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What are Ansible variable precedence rules?
Open an interactive chat with Bash
What is the purpose of `--extra-vars` in Ansible?
Open an interactive chat with Bash
How do group and host variables differ in Ansible?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access