Closed puriadeb closed 3 years ago
No. This is solvable perfectly fine right now if you use the inventory variables properly. The order of the variables in the inventory is:
inventory/group_vars/all/
inventory/group_vars/<group>/
inventory/host_vars/hostname/
With what you want to do, you can just setup a default layout in inventory/group_vars/all/
directory, and then override it in specific host directories using the same variable names. They will do exactly what you want, which is override global variables with specific variables.
@drybjed Not sure if understood the proposed solution. So explaining the problem a bit more. If I have "myVar=true" defined in vars section of main.yml, how can I overwrite it to "false" by means of some vars file (not commandline). That is without using "--extra-vars".
If we are talking about role variables, don't use vars/main.yml
file for them. Put the variables in defaults/main.yml
, in that case they can be easily overriden via Ansible inventory where you can have all/group/host granularity as you please.
Playbook variables are not as flexible, therefore it's best not to use them in this context. I'm not using them at all, and instead rely on role default variables.
https://docs.ansible.com/ansible/latest/reference_appendices/general_precedence.html <= clear 'configuration/settings' precedence rules, variables are part of it for a good reason and 'specificity' is normally associated with host > play.
The variable precedence (linked and referenced in subject) is orthogonal (it influences value of variable considered in configuration precedence) but does not determine the general precedence.
AS such I'm going to close this as 'completed', though it was unclear at the time, clear documentation has been provided now.
Proposal: Specific configuration must override generic configuration
Author: Debashis Prusty debashis.prusty@gmail.com IRC: handle (if different)
Date: 2018/29/06
Motivation
Generic variables defined within the playbook or provided via vars files are applicable for all hosts. But if the user wants some of the generic values (like interface name) to be different for a particular host, then there is no inbuilt mechanism to override that.
Problems
What problems exist that this proposal will solve?
In our playbooks we are importing all host specific variables at the end of setting variables section like below.
Solution proposal