我想在 Jinja2 的 Ansible 上下文中包含另一个 Jinja2 模板 [英] I want to include another Jinja2 template in an Ansible context in Jinja2
问题描述
我有一个 Ansible playbook,它设置了很多变量.一个剧本有这个任务:
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
是在父剧本中设置的 Ansible 变量.
job
is a Ansible variable set in a parent playbook.
这不起作用.可能是什么问题呢?
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屋!