I recently came across include_vars
as a method of allowing roles to load variables files based on facts, etc. However, I’m encountering an odd combination of issues in trying to structure my variables in a way that allows for proper overloading and (if possible) merging.
I notice that I can make use of with_first_found
in conjunction with include_vars
in my role’s task file(s) to fallback to sane defaults, eg.:
-
name: Include base yum repos
include_vars: “{{ item }}”
with_first_found: -
“…/vars/yum/{{ ansible_lsb.id|lower }}_{{ ansible_lsb.major_release }}.yml”
-
“…/vars/yum/{{ ansible_lsb.id|lower }}.yml”
-
“…/vars/yum/unknown.yml”
(as an aside, the requirement to provide a relative path, as it’s implemented via a lookup plugin seems to be obtuse and counter-intuitive, by the way, when compared to the way the path is properly resolved within the role’s vars/ directory with a single path, eg. “include_vars: ‘yum/centos.yml’”)
However, it would be nice to be able to merge in an additional default for all cases in a manner similar to what’s done for vars_files
in a playbook:
- name: Include base yum repos
include_vars: - “yum/default.yml”
- [“yum/{{ ansible_lsb.id|lower }}_{{ ansible_lsb.major_release }}.yml”, “yum/{{ ansible_lsb.id|lower }}.yml”, “yum/unknown.yml”]
Working similarly to vars_files
, this would treat an element with a list type as a first-found declaration.
I’m certain that there’s a purpose behind the implementation, so I’m curious what the reasons are behind the implementation and what gotchas would likely be lurking behind. I’ve already noticed that hashes aren’t merged (even with hash_behaviour
set to “merge” in the ansible.cfg) when attempting to work around this with multiple include_vars
actions:
- name: Include default yum repos
include_vars: “yum/default.yml” - name: Include base yum repos
include_vars: “{{ item }}”
with_first_found:
-
“…/vars/yum/{{ ansible_lsb.id|lower }}_{{ ansible_lsb.major_release }}.yml”
-
“…/vars/yum/{{ ansible_lsb.id|lower }}.yml”
-
“…/vars/yum/unknown.yml”
I’d gladly offer pull requests to perhaps make this more intuitive or consistent, if I could better understand the challenges involved in loading variables for a role this way.
Thanks!