I need to create some variables, lists, etc that are dynamically generated from other variables -- think of a list of nodes for kubernetes built with for loop.
This can be done inside Ansible with the set_fact
and looping through incrementally with a default()
, adding each list item each loop, but I don t want to build up all these vars with tasks.
I want to use a template to build the list and then use the result as variables. This works fine if I render the template to a file, and then load it into the playbook with vars_files
or include_vars
, but I wonder if it is possible to skip the intermediate file and do an in-place render-template realize-variables?
rendered_template.j2
nodes:
{% for item in nodes_struct %}
- name: "{{ item.name }}"
ipaddress: "{{ item.ipaddress }}"
networkifacename1: "enp1s0"
networkifacename2: "enp2s0"
{% endfor %}
playbook.yaml
- hosts: all
vars_files:
- ../vars/rendered_template.yaml
or
- name: Render global variables
include_vars: "../vars/rendered_template.yaml"