【问题标题】:Ansible - Jinja2 template with import encounters undefined variableAnsible - 带有导入的 Jinja2 模板遇到未定义的变量
【发布时间】:2021-05-07 09:35:34
【问题描述】:

我的剧本调用以下任务:

name: Mytask
    template:
       src: "path/to/template.j2"
       dest: "/dest/file"
       mode: '755'

此模板使用变量 {{ organization }} 然后使用关键字 with context

导入 jinja 宏
{% for organization in organization _list %}
  - name: Loading 
    include_vars:
      file: "{{ config_file_dir }}/{{ organization }}/{{ file_name }}"
      name: "platform_file"

{% from 'another_template.j2' import function_name as name with context %}
{{ name(param1, param2) }}


{% endfor %}

最后是导入的 jinja 宏,这里我在尝试打印时得到一个 未定义的变量 {{ organization }}

{% macro function_name(param1, param2) %}
{{ organization|pprint }}
{% endmacro %}

变量是在传递给第一个模板时定义的,但它不遵循该模板内的导入,与包含上下文的包含相同的问题。

我在版本 2.9.21 和 Python 2.7.12 中运行 ansible(需要 python 2.7 才能获得与 python 3 不兼容的库)

【问题讨论】:

标签: python ansible jinja2 ansible-template


【解决方案1】:

升级python和ansible后解决

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多