# Looping Issue making me Loopy!

**URL:** <https://forum.ansible.com/t/looping-issue-making-me-loopy/17389>\
**Category:** Ansible Project\
**Created:** [January 28, 2015, 2:11am UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389 "2015-01-28T02:11:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan\_Tahler](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@Evan\_Tahler](https://forum.ansible.com/u/Evan_Tahler)\
**Post date:** [January 28, 2015, 2:11am UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389/1 "2015-01-28T02:11:00Z")

</div>

Hi All! I cannot sort out how to itterate though this loop properly:

I have 2 data dictionaries:

applications:

- application\_1
- application\_2

and

big\_var\_collection:  
application\_1:  
deployment\_tasks:

- { cmd: ‘bundle exec rake thing:1’, metadata: ‘yay’ }
- { cmd: ‘bundle exec rake thing:2’, metadata: ‘boo’ }  
application\_2:  
deployment\_tasks:
- { cmd: ‘bundle exec rake thing:3’, metadata: ‘yay’ }
- { cmd: ‘bundle exec rake thing:4’, metadata: ‘boo’ }

In this example, I want to run 4 commands: cd into the directory and run the `cmd`, IE:

cd ~/www/projects/application\_1 && bundle exec rake thing:1

cd ~/www/projects/application\_1 && bundle exec rake thing:2

cd ~/www/projects/application\_2 && bundle exec rake thing:3

cd ~/www/projects/application\_2 && bundle exec rake thing:4

I would have assumed that the following loop would work, but I always end up with an access error of some sort

- name: run deployment tasks  
shell: \>  
cd ~/www/projects/{{ item.0 }} && {{ item.1.cmd }}  
with\_nested:
- applications
- big\_var\_collection[item.0].deployment\_tasks

Help?

---

<div class="post-metadata">

**Author:** ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://forum.ansible.com/u/michael1)\
**Post date:** [January 28, 2015, 6:28pm UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389/2 "2015-01-28T18:28:05Z")

</div>

Hello.

I’m wondering if this data stucture is something you’re getting back from another application or external source? If not, are you able to define the structure in anyway you desire? If not, why not?

Here is the code I managed to come up with, which has a much, much simpler data structure and does the same job.

`

---

<div class="post-metadata">

**Author:** ![Evan\_Tahler](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@Evan\_Tahler](https://forum.ansible.com/u/Evan_Tahler)\
**Post date:** [January 28, 2015, 6:43pm UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389/3 "2015-01-28T18:43:27Z")

</div>

The data structure is defined elsewhere, and it would easiest not to change it.

I’m currently migrating from Chef to Ansible, and the only pain point I’ve had has been the lack of data manipulation tools. In chef you have an (almost) full ruby VM to work with, so hash manipulation, loops, etc are all available to you.

What is the best practice in Ansible-land? Should I write a python plugin to manipulate the data structure? I assume that I’m just doing something wrong with my loop syntax…

---

<div class="post-metadata">

**Author:** ![Evan\_Tahler](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@Evan\_Tahler](https://forum.ansible.com/u/Evan_Tahler)\
**Post date:** [January 28, 2015, 9:18pm UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389/4 "2015-01-28T21:18:10Z")

</div>

It looks like with\_subelements is what I wanted:

- name: run deployment tasks  
shell: \>  
cd ~/www/projects/{{ item.0.name }} && {{ item.1.cmd }}  
with\_subelements:
- big\_var\_collection
- deployment\_tasks

I added a ‘name’ property to the hash (which was the same as the key) to make this work:

big\_var\_collection:  
application\_1:  
name: application\_1  
deployment\_tasks:

- { cmd: ‘bundle exec rake thing:1’, metadata: ‘yay’ }
- { cmd: ‘bundle exec rake thing:2’, metadata: ‘boo’ }  
application\_2:  
name: application\_2  
deployment\_tasks:
- { cmd: ‘bundle exec rake thing:3’, metadata: ‘yay’ }
- { cmd: ‘bundle exec rake thing:4’, metadata: ‘boo’ }

---

<div class="post-metadata">

**Author:** ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://forum.ansible.com/u/michael1)\
**Post date:** [January 29, 2015, 9:13am UTC](https://forum.ansible.com/t/looping-issue-making-me-loopy/17389/5 "2015-01-29T09:13:29Z")

</div>

So you CAN manipulate the data structure? Glad it’s working for you.
