Dans mon Ansible rôles, certains rôles dériver les paramètres de configuration spécifiques à partir de variables globales les variables Globales peuvent être indéfini. Le code suivant illustre le schéma:
- 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'
Tout fonctionne comme prévu, mais la dernière déclaration: Ansible soulève une exception parce que foo
n'est pas défini (oui, il n'est pas défini).
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"}
Alors, pourquoi ne bar
pas “évaluer” à undefined
comme foo
? Et comment puis-je piège de ce “multi-niveau” undefinedness?