# Looping over multiple sets of values

**URL:** <https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651>\
**Category:** Ansible Project\
**Created:** [July 7, 2012, 11:31pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651 "2012-07-07T23:31:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_Tirrell](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rob\_Tirrell](https://forum.ansible.com/u/Rob_Tirrell)\
**Post date:** [July 7, 2012, 11:31pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/1 "2012-07-07T23:31:28Z")

</div>

As far as I can tell, it’s currently impossible to loop over two sets of values in any fashion.

For each user I’m creating in a play, I’d like to install customizations to the user’s remote environment or fallback to a default. The way I’ve done this is by allowing a user to provide a file named as his or her username is a particular directory in source control. Essentialy:

- name: install customizations  
action: copy src=$available\_file dest=/home/$item/custom  
with\_items: $users  
first\_available\_file:
- res/users/$item
- res/users/default

Obviously, with this use case and probably most any other one one could work around the issue outside of ansible – and perhaps this goes too far in compromising the simplicity of the play syntax.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 7, 2012, 11:45pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/2 "2012-07-07T23:45:51Z")

</div>

> As far as I can tell, it's currently impossible to loop over two sets of values in any fashion.

AHA! YOU HAVE FALLEN INTO MY TRAP!!!!111

If you mean that with\_items and first\_available\_file don't work together, this is true, because they both use the variable $item.

This should probably have raised an error message or something, I didn't expect anyone to want to do it, though perfectly fine that you have tried.

(Sidenote, first\_available\_file is not a loop per se -- it selects the first file that exists on the local system, and passes that in for $item)

> For each user I'm creating in a play, I'd like to install customizations to the user's remote environment or fallback to a default. The way I've done this is by allowing a user to provide a file named as his or her username is a particular directory in source control. Essentialy:
> 
> - name: install customizations  
> action: copy src=$available\_file dest=/home/$item/custom  
> with\_items: $users  
> first\_available\_file:  
> - res/users/$item  
> - res/users/default
> 
> Obviously, with this use case and probably most any other one one could work around the issue outside of ansible -- and perhaps this goes too far in compromising the simplicity of the play syntax.

The variable used by $first\_available\_file could be changed to be "$file", perhaps.

Though this change would be incompatible with past play books, it wouldn't be catastrophic.

I would like to keep things simple enough where I don't have to say:

with\_items\_as\_foo: $splat  
first\_available\_file\_as\_bar: $glorp

It's hideous to explain and hard to read, so I think multiple "with\_items" loops are probably not going to happen.

Patches to change the "first\_available\_file" variable to "file", with updated tests, would definitely be accepted. We'd just have to make clear note of it in the release notes.

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [July 7, 2012, 11:53pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/3 "2012-07-07T23:53:55Z")

</div>

Could you perhaps do this with conditional imports instead? [http://ansible.github.com/playbooks2.html#conditional-imports](http://ansible.github.com/playbooks2.html#conditional-imports)

---

<div class="post-metadata">

**Author:** ![Rob\_Tirrell](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rob\_Tirrell](https://forum.ansible.com/u/Rob_Tirrell)\
**Post date:** [July 7, 2012, 11:57pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/4 "2012-07-07T23:57:36Z")

</div>

Another wrinkle is that, if you wanted to support multiple loops, they’d presumably have to be executed in the order given. Not impossible, but you’d have to load the YAML as an ordered dictionary.

As an aside, the unordered-ness of dictionaries is my biggest pet peeve and incites rage very frequently.

I’ll look into a patch (it could probably be made backwards-compatible by providing $file as $item if with\_items is not specified, yes?). Also, can anyone suggest the right way to maintain multiple pull requests on github? I’m kind of a dumb git.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 8, 2012, 12:10am UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/5 "2012-07-08T00:10:24Z")

</div>

> Another wrinkle is that, if you wanted to support multiple loops, they'd presumably have to be executed in the order given. Not impossible, but you'd have to load the YAML as an ordered dictionary.

That too. Though I hope there's not a huge case for needing that either. Would mean the configuration has taken on a life of it's own, I'd think 🙂

In the present case, with\_items is basically a macro that creates more than one task, and first\_available\_file is something that happens in the context of the task, so at least the order for those is pretty well defined.

> As an aside, the unordered-ness of dictionaries is my biggest pet peeve and incites rage very frequently.
> 
> I'll look into a patch (it could probably be made backwards-compatible by providing $file as $item if with\_items is not specified, yes?). Also, can anyone suggest the right way to maintain multiple pull requests on github? I'm kind of a dumb git. --

I'd find that confusing for the case where they were, so it's probably better if we just change it -- also makes future conversations easier.

As for github, I highly recommend starting a new branch for each feature request or idea -- and then you can delete those branches when they are no longer needed.

---

<div class="post-metadata">

**Author:** ![Rob\_Tirrell](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rob\_Tirrell](https://forum.ansible.com/u/Rob_Tirrell)\
**Post date:** [July 8, 2012, 4:10am UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/6 "2012-07-08T04:10:11Z")

</div>

I’m having a bit of trouble cracking the code - at what point is result of first\_available\_file bound to $item?

---

<div class="post-metadata">

**Author:** ![Rob\_Tirrell](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rob\_Tirrell](https://forum.ansible.com/u/Rob_Tirrell)\
**Post date:** [July 8, 2012, 4:29am UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/7 "2012-07-08T04:29:13Z")

</div>

OK, I get how it works now.

---

<div class="post-metadata">

**Author:** ![Rob\_Tirrell](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rob\_Tirrell](https://forum.ansible.com/u/Rob_Tirrell)\
**Post date:** [July 8, 2012, 5:26am UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/8 "2012-07-08T05:26:37Z")

</div>

# apply templating to source argument

inject = self.setup\_cache.get(conn.host,{})

# … find first available file

if self.module\_vars is not None:  
inject.update(self.module\_vars)

It looks like the solution could be as easy as moving the update statement above the first-available-file lookup – presuming that this isn’t bad in some way I haven’t grasped.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 8, 2012, 12:30pm UTC](https://forum.ansible.com/t/looping-over-multiple-sets-of-values/11651/9 "2012-07-08T12:30:47Z")

</div>

Let's continue the discussion here:

[https://github.com/ansible/ansible/issues/552](https://github.com/ansible/ansible/issues/552)
