我想在Jinja2的Ansible上下文中包含另一个Jinja2模板 [英] I want to include another Jinja2 template in an Ansible context in Jinja2
问题描述
我有一本Ansible剧本,其中设置了很多变量。其中一本剧本具有以下任务:
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"
模板 main_config.j2
写入在父Ansible剧本和任务中定义为变量的字符串。
The template main_config.j2
writes strings that are defined as variables in the parent Ansible playbooks and tasks.
我想基于Ansible变量的值包含另一个Jinja2模板。
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
是在
这不起作用。可能是什么问题呢?
This is not working. What could be the problem?
推荐答案
您无需打开Jinja2表达式( {{...}}
)以引用语句内的变量( {%...%}
)。您可以直接使用变量名称:
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' %}
这篇关于我想在Jinja2的Ansible上下文中包含另一个Jinja2模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!