I want to include another Jinja2 template in an Ansible context in Jinja2

谁说胖子不能爱 提交于 2019-11-30 04:37:11

问题


I have an Ansible playbook that sets a lot of variables. One the playbooks has this task:

- name: create config file 
  template:
    src: 'templates/main_config.j2'
    dest: "{{ tmp_dir }}/main_config.json"

The template main_config.j2 writes strings that are defined as variables in the parent Ansible playbooks and tasks.

I want to include another Jinja2 template based on a value of an Ansible variable.

{% include "./templates/configurations.j2" %}, 
{% include "./templates/security.j2" %},
{% include './templates/' + {{ job }} + '_steps.j2' %}

job is a Ansible variable set in a parent playbook.

This is not working. What could be the problem?


回答1:


You don't need to open a Jinja2 expression ({{ ... }}) to refer to a variable inside a statement ({% ... %}). You can use the variable name directly:

{% include './templates/' + job + '_steps.j2' %}


来源:https://stackoverflow.com/questions/42760061/i-want-to-include-another-jinja2-template-in-an-ansible-context-in-jinja2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!