the below block is an example block i use in a few places to copy in
config files and select a host specific file if it exists. not sure
if it's the best way, but it works for now. what i'd like to do is
add in group selection as well. ie if there's group file look for
that first
so above line 5 you could have
"files/{{ansible_local.baseos.ver}}/{{item.src}}_AG{{group}}" but
clearly that wont work because there likely is more then on group
attached to a host. so i need to try all the groups of a host and see
if there's a matching file. the only way i can think to do it is to
create a second task that looks in the repository for a matching group
file and then registers a variable which i can include in the below
block above line 5
It seems to me that you’re re-implementing variable precedence, but only for one variable.
What if, in group_vars/all, you define
access_conf_src_file: access.conf
Then in your group_vars/whatever (for relevant groups of course, for which the other one isn’t specific enough), you define
access_conf_src_file: access.conf_AGwhatever
Finally, if you have any hosts which need even more specificity, you define in your host_vars/snowflake1.your.dom
access_conf_src_file: access.conf_AHsnowflake
Then your copy task can just use “{{ access_conf_src_file }}” without invoking magical expressions.
Admittedly, the appeal of having the Right Thing happen just by creating the appropriately named src file is compelling. But it isn’t the way the rest of your playbook variables work, so, hmm.
But next, I start to wonder if the right answer isn’t to put all the logic and magic in a template and use ansible.builtin.template instead of ansible.builtin.copy.
My sense is they are searching from most specific to least specific file. The first one they find they use. The remaining params are hardcoded. We do a similar thing when looking for vars files. We might search for RedHat8, then RedHat, then Linux.
name: Add OS specific variables for {{ ansible_os_family }} family
include_vars: “{{ loop_vars }}”
with_first_found: