ansible / proposals

Repository for sharing and tracking progress on enhancement proposals for Ansible.
Creative Commons Zero v1.0 Universal
93 stars 19 forks source link

Specific configuration must override generic configuration #122

Closed puriadeb closed 3 years ago

puriadeb commented 6 years ago

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

drybjed commented 6 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:

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.

puriadeb commented 6 years ago

@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".

drybjed commented 6 years ago

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.

bcoca commented 3 years ago

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.