Ansible: 'dict object' no tiene ningún atributo en when cláusula 2.3 RC1

Creado en 17 mar. 2017  ·  2Comentarios  ·  Fuente: ansible/ansible

TIPO DE PROBLEMA
  • Informe de error
NOMBRE DEL COMPONENTE

núcleo

VERSION ANSIBLE
ansible 2.3.0.0 (stable-2.3 923c9ef17c) last updated 2017/03/17 09:12:40 (GMT +1000)
  config file = /apps/dev/playbook/jordan/mr-ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Sep 23 2016, 14:23:49) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
CONFIGURACIÓN

Estándar

SO / MEDIO AMBIENTE

RHEL 6.7

RESUMEN

Esto puede no ser un error, sino algo que me salí con la mía en versiones anteriores, pero con Ansible 2.3 RC1 si estoy verificando si una variable registrada por un módulo (con una cláusula when) está definida y luego obtengo un valor de sub dictado falla en 2.3 mientras que en 2.2 y por debajo funciona bien. Esto sucede específicamente cuando el módulo que registra la variable se omite por cualquier motivo y luego una tarea posterior verifica si la variable está definida y luego obtiene la subclave.

PASOS PARA REPRODUCIR

Libro de jugadas de ejemplo

---
- name: test
  hosts: localhost
  connection: local

  tasks:
  - name: module that won't run
    stat:
      path: /apps/tmp
    register: test
    when: False

  - name: debug of var
    debug:
      var: test

  - name: test with conditional
    debug:
      msg: A
    when: test is defined and test.stat.exists
RESULTADOS PREVISTOS

Al usar Ansible 2.2.1.0

ansible 2.2.1.0
  config file = /cygdrive/d/dev/jordan/ansible.cfg
  configured module search path = Default w/o overrides
PLAY [test] ********************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [module that won't run] ***************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:7
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [debug of var] ************************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:13
ok: [localhost] => {
    "test": {
        "changed": false,
        "skip_reason": "Conditional check failed",
        "skipped": true
    }
}

TASK [test with conditional] ***************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:17
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
RESULTADOS ACTUALES
- ¿Lo que realmente pasó? Si es posible, ejecute con más verbosidad (-vvvv)

PLAY [test] ************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************
Friday 17 March 2017  15:20:42 +1000 (0:00:00.039)       0:00:00.039 **********
ok: [localhost]
META: ran handlers

TASK [module that won't run] *******************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:7
Friday 17 March 2017  15:20:44 +1000 (0:00:01.093)       0:00:01.133 **********
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}

TASK [debug of var] ****************************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:13
Friday 17 March 2017  15:20:44 +1000 (0:00:00.012)       0:00:01.145 **********
ok: [localhost] => {
    "changed": false,
    "test": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}

TASK [test with conditional] *******************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:17
Friday 17 March 2017  15:20:44 +1000 (0:00:00.034)       0:00:01.180 **********
fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'test is defined and test.stat.exists' failed. The error was: error while evaluating conditional (test is defined and test.stat.exists): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/apps/dev/playbook/jordan/mr-ansible/test.yml': line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: test with conditional\n    ^ here\n"}

PLAY RECAP *************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1
affects_2.3 bug

Comentario más útil

Posible malentendido

¡Hola!

Muchas gracias por su envío a Ansible. Sinceramente, significa mucho para nosotros.

Creemos que la multa que ha presentado se está malinterpretando un poco, ya que una cosa funciona de manera un poco diferente a la indicada.

En el futuro, este podría ser un tema más adecuado para la lista de usuarios, que también puede publicar aquí si desea obtener más ayuda con lo anterior.

Dado que está omitiendo la tarea 'stat', aún obtiene test como un diccionario definido, pero no tiene una clave stat por lo que obtiene el mensaje de error CORRECTO. Para evitar problemas como este, necesita:

  when: test is defined and 'stat' in test and test.stat.exists

¡Gracias una vez más por esto y su interés en Ansible!

Todos 2 comentarios

Posible malentendido

¡Hola!

Muchas gracias por su envío a Ansible. Sinceramente, significa mucho para nosotros.

Creemos que la multa que ha presentado se está malinterpretando un poco, ya que una cosa funciona de manera un poco diferente a la indicada.

En el futuro, este podría ser un tema más adecuado para la lista de usuarios, que también puede publicar aquí si desea obtener más ayuda con lo anterior.

Dado que está omitiendo la tarea 'stat', aún obtiene test como un diccionario definido, pero no tiene una clave stat por lo que obtiene el mensaje de error CORRECTO. Para evitar problemas como este, necesita:

  when: test is defined and 'stat' in test and test.stat.exists

¡Gracias una vez más por esto y su interés en Ansible!

¿Fue útil esta página
0 / 5 - 0 calificaciones