11

我有一个设置很多变量的 Ansible 剧本。其中一个剧本有这个任务:

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

模板main_config.j2写入在父 Ansible playbook 和任务中定义为变量的字符串。

我想包含另一个基于 Ansible 变量值的 Jinja2 模板。

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

job是在父 playbook 中设置的 Ansible 变量。

这是行不通的。可能是什么问题呢?

4

1 回答 1

23

您无需打开 Jinja2 表达式 ( {{ ... }}) 即可引用语句 ( {% ... %}) 中的变量。可以直接使用变量名:

{% include './templates/' + job + '_steps.j2' %}
于 2017-03-13T09:41:54.067 回答