繁体   English   中英

如何用 Jinja2 继承未定义的变量?

[英]How to inherit undefined variables with Jinja2?

在我的 Ansible 角色中,某些角色从全局变量派生特定配置设置 全局变量可能未定义。 以下代码说明了架构:

- hosts: localhost
  vars:
    bar: '{{ foo }}'
  tasks:
    # Assume foo comes from an Ansible environment
    - debug: var=foo
    # Assume bar comes from a role default
    - debug: var=bar
    # Catched by the "is defined" condition
    - debug: msg="foo is defined"
      when: 'foo is defined'
    # Cannot catch undefined exception?!
    - debug: msg="bar is defined"
      when: 'bar is defined'

一切都按预期工作,但最后一条语句: Ansible 引发异常,因为foo未定义(是的,它未定义)。

PLAY [localhost] *********************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [localhost]

TASK [debug] *************************************************************************************************************************************************************
ok: [localhost] => {
    "foo": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] *************************************************************************************************************************************************************
ok: [localhost] => {
    "bar": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] *************************************************************************************************************************************************************
skipping: [localhost]

TASK [debug] *************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'bar is defined' failed. The error was: error while evaluating conditional (bar is defined): {{ foo }}: 'foo' is undefined\n\nThe error appears to be in '.../test-undef.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      when: 'foo is defined'\n    - debug: msg=\"bar is defined\"\n      ^ here\n"}

那么为什么bar不像foo那样“评估”为undefined呢? 我怎样才能捕捉到这种“多层次”的不确定性?

尝试这个:

- hosts: localhost
  vars:
    bar: '{{ foo }}'
  tasks:
    # Assume foo comes from an Ansible environment
    - debug: var=vars.foo
    # Assume bar comes from a role default
    - debug: var=vars.bar
    # Catched by the "is defined" condition
    - debug: msg="foo is defined"
      when: vars.foo is defined
    # Cannot catch undefined exception?!
    - debug: msg="bar is defined"
      when: vars.bar is defined

尝试添加

when: ( vars[bar] is defined )

问题是 bar 是在技术上定义的,而您对bar的定义使用了一个可能未定义的变量。 当您尝试对bar执行任何操作时,必须将其评估为独立的 Jinja 表达式,这发生在is defined检查之前。

解决此问题的一种方法是使其可以评估bar而不会导致未定义的值,例如

- hosts: localhost
  vars:
    bar: "{{ foo | default(false) }}"
  tasks:
    - debug:
        msg: bar is truthy
      when: bar is truthy

您还可以在 bar 之前检查 foo,因为评估是可短路的,但是将变量关系的知识融入到您的任务中可能会很笨拙。

- hosts: localhost
  vars:
    bar: "{{ foo }}"
  tasks:
    - debug:
        msg: bar is truthy
      when: 
        - foo is defined
        - bar is defined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM