Ansible: 'dict object' has no attribute in when clause 2.3 RC1

Created on 17 Mar 2017  ·  2Comments  ·  Source: ansible/ansible

ISSUE TYPE
  • Bug Report
COMPONENT NAME

core

ANSIBLE VERSION
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)]
CONFIGURATION

Standard

OS / ENVIRONMENT

RHEL 6.7

SUMMARY

This may not be a bug but just something I got away with on older versions but with Ansible 2.3 RC1 if I am checking if a variable registered by a module (with an when clause) is defined and then subsequently get a sub dict value it will fail in 2.3 while in 2.2 and below it works fine. This specifically happens when the module that registers the variable is skipped for any reason and then a subsequent task checks if the variable is defined and then gets the sub key.

STEPS TO REPRODUCE

Example playbook

---
- 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
EXPECTED RESULTS

When using 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
ACTUAL RESULTS

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

Most helpful comment

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Since you are skipping the 'stat' task, you still get test as a defined dictionary, but it does not have a stat key which is why you get the CORRECT error message. To avoid issues like this you need:

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

Thank you once again for this and your interest in Ansible!

All 2 comments

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Since you are skipping the 'stat' task, you still get test as a defined dictionary, but it does not have a stat key which is why you get the CORRECT error message. To avoid issues like this you need:

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

Thank you once again for this and your interest in Ansible!

Was this page helpful?
0 / 5 - 0 ratings