Come ereditare variabili non definite con Jinja2?

0

Domanda

Nel mio Ansible ruoli, alcuni ruoli derivare impostazioni di configurazione specifiche da variabili globali le variabili globali possono essere indefinito. Il codice riportato di seguito viene illustrato lo 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'

Tutto funziona come previsto, ma l'ultima dichiarazione: Ansible solleva un'eccezione, perché foo è indefinito (sì, non è definito).

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"}

Quindi perché non bar non “valutare” a undefined come foo? E come posso bloccare questa “multi-livello” undefinedness?

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

Prova questo:

- 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: Sì, infatti... E se si esegue il mio script, quindi vedrete che funziona bene...
Swifty

sì...sono d'accordo con te,
Frenchy

Grazie, @Swifty. Tuttavia, Ansible mostra vars.bar come definito con il tuo codice: text TASK [debug] ************************************************************************************************************************************************************* ok: [localhost] => { "msg": "bar is defined" } Probabilmente perché contiene il modello di stringa?
Stephan

sì, il suo diritto, così pippo è definito implica bar definito
Frenchy

Ma foo non è definito ...
Stephan

pippo non definito implica bar non definita..devi solo controllare se pippo è definito per verificare se il bar è definito
Frenchy

@Stefano, si Prega di capire la differenza tra '{{ foo }}' e "{{ foo }}"... "{{ foo }}" significa che una variabile... '{{ foo }}' letteralmente significa che la stringa...
Swifty
0

Il problema è che il bar è tecnicamente definito, e la tua definizione di bar utilizza una possibile variabile non definita. Quando si tenta di fare qualcosa con bar esso deve essere valutato come un indipendente Jinja espressione, che avviene prima che il is defined di controllo.

Un modo per affrontare questo è quello di rendere in modo bar possono essere valutate senza determinando un valore indefinito, ad es.

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

È inoltre possibile controllare pippo prima di bar in quanto la valutazione è di breve circuitable, ma la cottura conoscenza della variabile in relazione le attività possono essere pesanti.

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

Si prega di notare che @Stephan definito il bar variabile è una stringa! bar: '{{ foo }}' letteralmente significa sting!!!
Swifty
-1

prova ad aggiungere

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

vars è un'privi di documenti di attuazione interna che può essere rimosso in futuro e non deve essere utilizzato.
flowerysong

In altre lingue

Questa pagina è in altre lingue

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