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

0

的问题

我在你的角色,一些角色获得具体结构的设置,从全球变量的全球变量的可能定义。 以下代码说明的架构:

- 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'

一切都如预期,但最后声明:需求提出了一个例外,因为 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 不是"评价", undefined 喜欢 foo? 我怎么能陷阱这个"多层次"undefinedness?

ansible jinja2
2021-11-23 09:46:53
3
0

试试这个:

- 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
2021-11-23 09:57:21

@法国佬:是的,确实...如果你跑我的脚然后你会看到它运行良好...
Swifty

是的...我同意你的
Frenchy

谢谢,@枪手 但是你展示了 vars.bar 作为 定义 与你的代码: text TASK [debug] ************************************************************************************************************************************************************* ok: [localhost] => { "msg": "bar is defined" } 可能因为它拥有的模板串?
Stephan

是的它的权利,所以foo定义意味着条定义
Frenchy

foo 是不是定义...
Stephan

foo不一定意味着条没有定义..你只需要检查如果foo是界定来检查,如果条定义
Frenchy

@斯蒂芬,请理解之间的差异 '{{ foo }}'"{{ foo }}"... "{{ foo }}" 意味着变量... '{{ foo }}' 字面意思是这一串...
Swifty
0

问题是那个酒吧在技术上是定义的,和您的定义 bar 使用可能是不确定的变量。 当你尝试做任何事情 bar 它必须评价为一个独立的神表达,这种情况发生之前 is defined 检查。

一种方法来解决这是为了使它如此 bar 可以评估没有发生在一个未定义的价值,例如

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

你还可以检查foo前的酒吧,因为评价短circuitable,但烘烤知识的变量的关系进入你的任务可能难以推行。

- hosts: localhost
  vars:
    bar: "{{ foo }}"
  tasks:
    - debug:
        msg: bar is truthy
      when: 
        - foo is defined
        - bar is defined
2021-11-23 17:15:56

请注意,@Stephan定义的条变为一个弦! bar: '{{ foo }}' 字面上的意思是一个刺!!!
Swifty
-1

尝试加入

when: ( vars[bar] is defined )
2021-11-23 15:28:45

vars是无证件的内部实现,也可以删除,在未来,而不应被使用。
flowerysong

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................