# Looping over subelements

**URL:** <https://forum.ansible.com/t/looping-over-subelements/24000>\
**Category:** Ansible Developer\
**Created:** [March 17, 2017, 5:13pm UTC](https://forum.ansible.com/t/looping-over-subelements/24000 "2017-03-17T17:13:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Allen](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Jason\_Allen](https://forum.ansible.com/u/Jason_Allen)\
**Post date:** [March 17, 2017, 5:13pm UTC](https://forum.ansible.com/t/looping-over-subelements/24000/1 "2017-03-17T17:13:45Z")

</div>

Hello,

I’m unable to get the “looping over subelements” example on this page:  
[http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements)

to work. Specifically, when I copy-and-paste “users” into my file “group\_vars/test” and attempt to run:

- debug:  
msg:
- “{{ item.0.name }}”
- “{{ item.0.mysql.password }}”
- “{{ item.1 }}”
- “{{ item.0.mysql.privs | join(‘/’) }}”  
with\_subelements:
- “{{ users }}”
- “{{ mysql.hosts }}”

ansible-playbook reports:

[DEPRECATION WARNING]: Skipping task due to undefined Error, in the future this will be a fatal error.: ‘mysql’ is undefined.  
This feature will be removed in a future  
release. Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.  
An exception occurred during task execution. The full traceback is:  
Traceback (most recent call last):  
File “/opt/Python-2.7.6/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 91, in run  
items = self.\_get\_loop\_items()  
File “/opt/Python-2.7.6/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 191, in \_get\_loop\_items  
items = self.\_shared\_loader\_obj.lookup\_loader.get(self.\_task.loop, loader=self.\_loader, templar=templar).run(terms=loop\_terms, variables=self.\_job\_vars, wantlist=True)  
File “/opt/Python-2.7.6/lib/python2.7/site-packages/ansible/plugins/lookup/subelements.py”, line 37, in run  
terms[0] = listify\_lookup\_plugin\_terms(terms[0], templar=self.\_templar, loader=self.\_loader)  
IndexError: list index out of range

fatal: [[imdb410a.unx.sas.com](http://imdb410a.unx.sas.com)]: FAILED! =\> {“failed”: true, “msg”: “Unexpected failure during module execution.”, “stdout”: “”}

I don’t want to set up any mysql users. I want an example to work with because I want to use a similar data structure in our playbooks.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 17, 2017, 7:09pm UTC](https://forum.ansible.com/t/looping-over-subelements/24000/2 "2017-03-17T19:09:52Z")

</div>

The group “all” is implicit for every host in the inventory file, which is why the example works. You chose to define the users in the group\_var “test”, therefore your host needs to belong to also belong to that group. Add the host you’re running this playbook against to a group called test in your inventory file and it should work:

[test]  
localhost  
host1

Am I wrong to think this should have been asked in Ansible Project and not Ansible Development?

---

<div class="post-metadata">

**Author:** ![Jason\_Allen](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Jason\_Allen](https://forum.ansible.com/u/Jason_Allen)\
**Post date:** [March 22, 2017, 5:52pm UTC](https://forum.ansible.com/t/looping-over-subelements/24000/3 "2017-03-22T17:52:31Z")

</div>

The host I’m running this playbook against is in a group called “test” in my inventory file:

[test]  
[server.example.com](http://server.example.com) ansible\_connection=ssh

To answer your question, I followed the link to the “User Mailing List” at the bottom of this page:  
[http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements)

which states: “Have a question? Stop by the google group!” Perhaps that should link to group “Ansible Project” (if that’s the desired intent), or it should be named “Developer Mailing List” to make it clear it’s intended for ansible developers, not users.

I’ll follow up on ansible-project. Thanks.
