En mi Ansible funciones, algunas funciones se derivan de configuración específica de variables globales las variables globales pueden ser indefinido. El código siguiente ilustra el esquema:
- 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'
Todo funciona como se espera, pero la última instrucción: Ansible plantea una excepción, porque foo
no está definido (sí, es indefinido).
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"}
Entonces, ¿por qué bar
no de "evaluar" a undefined
como foo
? Y ¿cómo puedo trampa de esta "multi-nivel" undefinedness?