Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'dict object' has no attribute in when clause 2.3 RC1 #22729

Closed
jborean93 opened this issue Mar 17, 2017 · 2 comments
Closed

'dict object' has no attribute in when clause 2.3 RC1 #22729

jborean93 opened this issue Mar 17, 2017 · 2 comments
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.

Comments

@jborean93
Copy link
Contributor

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
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report needs_triage Needs a first human triage before being processed. labels Mar 17, 2017
@jctanner jctanner removed the needs_triage Needs a first human triage before being processed. label Mar 17, 2017
@bcoca
Copy link
Member

bcoca commented Mar 17, 2017

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!

@bcoca bcoca closed this as completed Mar 17, 2017
@jctanner
Copy link
Contributor

jctanner commented Mar 17, 2017

Bisected commit that changed this behavior 81aa12e
Related issue #18514

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

4 participants