I'm trying to recursively import variables from folders with the following structure:
containers
|_ deluge
|_ defaults
|_ main.yml
|_ tasks
|_ main.yml
|_ jellyfin
|_ defaults
|_ main.yml
|_ tasks
|_ main.yml
Each folder has a default/main.yml file that looks like this:
---
container_name: deluge
port: 8112
homer_url: "http://{{ ansible_host }}:{{ port }}"
These variables have the same names across all folders.
I would like to import the homer_url variables into a playbook in the following form, so that I can address them by using homer_containers.deluge, homer_containers.jellyfin, and so on:
homer_containers {
deluge: "http://myhost.com:8112"
plex: "http://myhost.com:32400"
jellyfin: "http://myhost.com:8096"
}
Here's how I'm trying to do it:
---
- name: Get a list of containers
delegate_to: localhost
become: no
find:
paths:
- "roles/containers"
file_type: directory
excludes: "homer"
recurse: no
register: containers
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
include_vars:
dir: "{{ playbook_dir }}/{{ item.path }}/defaults"
files_matching: main.yml
name: "{{ item.path.split('/')[-1] }}"
with_items: "{{ containers.files }}"
no_log: true
- name: print out
debug:
var: deconz.homer_url
- name: Populate the dictionary
set_fact:
homer_containers: "{{ homer_containers | default({}) | combine ( { item.path.split('/')[-1] : vars[item.path.split('/')[-1] + '.homer_url'] } ) }}"
cacheable: true
with_items: "{{ containers.files }}"
- name: print out
debug:
var: homer_containers
However, Ansible fails to resolve the variable name into an actual variable:
TASK [containers/homer : print out]
ok: [myhost] => {
"deconz.homer_url": "http://myhost.com:8085"
}
TASK [containers/homer : Populate the dictionary]
fatal: [myhost]: FAILED! => {"msg": "The task includes an option with an undefined variable.
The error was: 'dict object' has no attribute 'deconz.homer_url'}