Jinja 模板作为变量值

时间:2021-05-05 16:06:42

标签: templates ansible jinja2

我正在尝试使用 模板作为角色任务中 ansible.builtin.lineinfile module 的值。

named
├── tasks
│   ├── config.yml
│   ├── main.yml
├── templates
│   ├── option.ji2
│   ├── view.ji2
  ansible.builtin.lineinfile:
    path: "{{ test_dir }}{{ named_config_file['path'] }}"
    regex: "{{ item['key'] }}"
    line: "{% set option = item %}{% include 'option.ji2' %}"
    validate: named-checkconf %s
  loop: "{{ named_options | dict2items }}"

不幸的是 ansible 抛出 TemplateNotFound 错误:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: jinja2.exceptions.TemplateNotFound: option.ji2

使用模板的相对或绝对路径没有帮助。

view.ji2 中包含的相同模板有效:

{% for option in item['options'] | dict2items %}
{% if option is mapping %}
{% include 'option.ji2' %}
{% endif %}
{% endfor %}

在其他角色手册中,有一个简单的 Jinja 模板(没有包含)也可以使用:

  ansible.posix.authorized_key:
    key:
      "{% if item[1]['key'] is match('ssh-.{256}') %}\
      {{ item[1]['key'] }}\
      {% else %}\
      {{ lookup('file', item[1]['key'], errors='ignore') }}\
      {% endif %}"
    user: "{{ item[0]['name'] }}"
    state: "{{ item[1]['state'] | default(omit, true) }}"
  loop: "{{ user_accounts | subelements('authorized_keys', skip_missing=True) }}"

知道为什么 找不到/使用模板 option.ji2 吗?

0 个答案:

没有答案
相关问题