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

Bug Report: Trying to resolve a dictionary key from an empty list #10920

Closed
Rylon opened this issue May 5, 2015 · 6 comments
Closed

Bug Report: Trying to resolve a dictionary key from an empty list #10920

Rylon opened this issue May 5, 2015 · 6 comments
Labels
bug This issue/PR relates to a bug.

Comments

@Rylon
Copy link
Contributor

Rylon commented May 5, 2015

Issue Type
Bug Report

Ansible Version
ansible 1.9.1
configured module search path = None

Environment
Running from Mac OS X 10.10.3, managing Ubuntu 14.04 LTS.

Summary
Using with_items and an empty list, the task fails if the list is expected to contain dictionaries, which you try to lookup by key.

Steps To Reproduce
Consider this task:

- name: Install custom Elasticsearch plugins
  command:    /usr/share/elasticsearch/bin/plugin --install {{ item.name }} creates="/usr/share/elasticsearch/plugins/{{ item.installs_to }}"
  with_items: elasticsearch_plugins

When the elasticsearch_plugins variable is not defined, or is defined as an empty list ([]), I would expect the entire task to be skipped, however it looks like maybe Ansible is still trying to lookup the name key from item, which won't exist if the list is empty, and this results in the following failure:

fatal: [ubuntu_trusty] => One or more undefined variables: 'unicode object' has no attribute 'name'

When elasticsearch_plugins is defined as follows, this task works normally:

elasticsearch_plugins:
  - name:        "mobz/elasticsearch-head"
    installs_to: "head"

Expected Results
When the elasticsearch_plugins variable is an empty list, this task should be skipped, without attempting to lookup item.name or item.installs_to.

Actual Results
The task fails, with the following error:

fatal: [ubuntu_trusty] => One or more undefined variables: 'unicode object' has no attribute 'name'

@mightydok
Copy link
Contributor

@bcoca skiping task is correct behaviour for empy iterator?

@bcoca
Copy link
Member

bcoca commented May 11, 2015

@mightydok yes, with_ should skip if given None/Empty list

@jimi-c
Copy link
Member

jimi-c commented Jul 13, 2015

In the current devel branch, an empty list does result in a skipped task:

# ansible-playbook -vv 10920.yml -e elasticsearch_plugins=[]
Using /etc/ansible/ansible.cfg as config file
1 plays in 10920.yml
PLAY: ***************************************************************************
TASK [iterate over an non-existent variable] ************************************
ok: [localhost] => {"changed": false, "results": [], "skipped": true, "skipped_reason": "No items in the list"}
PLAY RECAP **********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0   

Dealing with a non-existent variable is a bit more complex, since we have traditionally allowed bare-variables in with_* loops, however we have discussed deprecating that in favor of forcing something like with_item: "{{foo}}" instead, to make it clear that there is a variable there and not a string.

Per our normal policy, we close bug report issues when we see the issue has been corrected in the devel branch, so we will close this issue now.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

@jimi-c jimi-c closed this as completed Jul 13, 2015
@Rylon
Copy link
Contributor Author

Rylon commented Jul 14, 2015

@jimi-c thanks! I'm not 100% familiar with the development process, but what sort of time period are we talking about for going from current devel branch into a release?

@jimi-c
Copy link
Member

jimi-c commented Jul 14, 2015

@Rylon we're looking at releasing an official 2.0 beta in early August. Major releases are generally done every three months, however we're going a bit slower with 2.0 as it is a fairly major change.

@Rylon
Copy link
Contributor Author

Rylon commented Jul 14, 2015

@jimi-c Sounds great, thanks :)

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

No branches or pull requests

5 participants