После долгих исследований я пришел к такому подходу к созданию групповых переменных, которых я ожидал. Я взял части ответа из множества источников, которые, к сожалению, больше не могу назвать подробно.
hosts.yml
---
all:
hosts:
computenode[1:2]:
children:
computenodes:
hosts:
computenode[1:2]:
net_192_168_99:
children:
computenodes:
net_192_168_123:
children:
computenodes:
...
group_vars / net_192_168_99.yml
---
net_addr: 99
net_mask: 24
...
group_vars / net_192_168_123.yml
---
net_addr: 123
net_mask: 24
...
playbook.yml
---
- name: Debug playbook
hosts: 127.0.0.1
become: false
vars:
groupvars: {}
tasks:
- name: Load groupvars
include_tasks: loadvars.yml
vars:
group_name: "{{ item }}"
loop: "{{ groups | select('match','net_.*')| list }}"
- name: List groupvars
debug:
var: "{{ item }}"
become: false
loop: "{{ groupvars | list }}"
when: item is defined
- name: Create from template
template:
src: debug.j2
dest: debug.txt
...
loadvars.yml
---
- name: Check if file exist in group_vars
stat:
path: group_vars/{{ group_name }}.yml
register: result
- name: Load vars if exist
include_vars:
file: group_vars/{{ group_name }}.yml
name: "{{ group_name }}"
become: false
when: result.stat.exists
register: foo
- name: Add to groupvars{}
set_fact:
groupvars: "{{ groupvars | combine(foo.ansible_facts) }}"
cacheable: yes
become: false
when: result.stat.exists
...
шаблоны / debug. j2
{% for item in groupvars|dict2items %}
{{ item.key }}
{% for inner in item.value|dict2items %}
{{ inner.key }} {{ inner.value }}
{% endfor %}
{% endfor %}