English 中文(简体)
Include variables into a playbook from an in-place rendered jinja2 template without intermediate file
原标题:

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"
问题回答

暂无回答




相关问题
Strip whitespace in generated HTML using pure Python code

I am using Jinja2 to generate HTML files which are typically very huge in size. I noticed that the generated HTML had a lot of whitespace. Is there a pure-Python tool that I can use to minimize this ...

String concatenation in Jinja

I just want to loop through an existing list and make a comma delimited string out of it. Something like this: my_string = stuff, stuff, stuff, stuff I already know about loop.last, I just need to ...

How do you sort a list in Jinja2?

I am trying to do this: {% for movie in movie_list | sort(movie.rating) %} But that s not right...the documentation is vague...how do you do this in Jinja2?

How to get django context automatically in Jinja2 filters?

For example, I have an paginator object with a lot of attributes, and don t want do write something like {{ paginate(paginator) }} in templates. How can a get context automatically in the filter ...

热门标签