상속하는 방법을 정의되지 않은 변수 Jinja2?

0

질문

내 Ansible 역할,일부 역할을 파생하는 특정 구성에서 설정을 전역변수 글로벌 변수가 될 수도 정의되지 않습니다. 다음 코드를 보여 줍니다 schema:

- 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 지 않은"평가려면" undefinedfoo? 할 수 있는 방법이"멀티-레벨"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

@Frenchy:네,그렇다면 당신은 실행 스크립트는 다음 당신이 볼 것 그것을 실행한...
Swifty

예...나는 당신
Frenchy

감사,@니다. 그러나,Ansible 보여줍 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

주@스테판 정의됩 변수 문자열로! 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ý
..................................................................................................................