# with\_items -- from var

**URL:** <https://forum.ansible.com/t/with-items-from-var/11485>\
**Category:** Ansible Project\
**Created:** [May 2, 2012, 1:47am UTC](https://forum.ansible.com/t/with-items-from-var/11485 "2012-05-02T01:47:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 1:47am UTC](https://forum.ansible.com/t/with-items-from-var/11485/1 "2012-05-02T01:47:48Z")

</div>

Earlier today I brought up on IRC a question about with\_items taking a var list (rather than literal). It doesn’t sound like this is a desirable feature, so I’m turning to the list to ask for suggestions.

I need to pass in (via extra\_vars most likely) some transient data, which is actually a list. For each item in that list, I need to perform a certain task. Any suggestions how to do that without having to invoke ansible-playbook once for each item in that list (which, in my case, would work).

---

<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:** [May 2, 2012, 1:52am UTC](https://forum.ansible.com/t/with-items-from-var/11485/2 "2012-05-02T01:52:33Z")

</div>

Yeah, it’s desirable, the question is how to do it cleanly without introducing weird syntax. I think the question in IRC I originally bristled to was suggesting

{{ list\_var }}

Maybe not. Anyway, that’s a template thing, and that’s kind of tricky.

However, is it really a template thing?

with\_items: $list\_var

In this case, we would NOT want to do utils.template, but would need to note that this was a variable, and in which case use (but not template) the variable.

So we could easily enough check if something was available in vars.

The question though, is how you do it, so people don’t infer it works in a way different than it actually works, and expect something more complicated to also work, when it might not 🙂

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 2:00am UTC](https://forum.ansible.com/t/with-items-from-var/11485/3 "2012-05-02T02:00:57Z")

</div>

Would it be bad to support $items but not {{ items }}?

I’m fine with:

with\_items: $items

which is enough different from:

with\_items:

- item1
- item2
- item3

As to easily know not to template it… as:

with\_items: foo

…would be illegal anyway, so templating the single value of with\_items would not make sense. But I do see your point… so I’ll think some more about alternate 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:** [May 2, 2012, 2:19am UTC](https://forum.ansible.com/t/with-items-from-var/11485/4 "2012-05-02T02:19:23Z")

</div>

> Would it be bad to support $items but not {{ items }}?

I think it’s much easier to only do $items, yes. I have a slight concern it becomes murky what’s templating and what is not, BUT, as long as we stress that play books are not Jinja2 templates, that is mostly ok.

In this case, we’d throw away the “$” and look for a variable, and explode otherwise.

It’s somewhat a lie, but not a terrible one.

Template evaluation in playbooks is solely there to replace variables, so if this particular variable replaces a variable in a completely different way, I think that’s acceptable, as long nobody has to know 🙂

> I’m fine with:
> 
> with\_items: $items
> 
> which is enough different from:
> 
> with\_items:
> 
> - item1
> - item2
> - item3
> 
> As to easily know not to template it… as:
> 
> with\_items: foo

right, this would be too foreign from other things to be valid syntax, IMHO.

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 2:24am UTC](https://forum.ansible.com/t/with-items-from-var/11485/5 "2012-05-02T02:24:42Z")

</div>

OK, I’ll tackle this now. Shouldn’t be hard. $items only though.

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 2:25am UTC](https://forum.ansible.com/t/with-items-from-var/11485/6 "2012-05-02T02:25:43Z")

</div>

Of course, there’s another issue… which is that --extra-vars (the most likely place to get this list from) would more likely take a comma delimited list, rather than yaml.

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 2:30am UTC](https://forum.ansible.com/t/with-items-from-var/11485/7 "2012-05-02T02:30:54Z")

</div>

Should it first try to load $items as yaml, and then try items.split(“,”)?

---

<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:** [May 2, 2012, 2:34am UTC](https://forum.ansible.com/t/with-items-from-var/11485/8 "2012-05-02T02:34:14Z")

</div>

So all the vars, if you do it in the right place, should be accessible in the data structure vars.

In which case, if the value of with\_items is a string, and it starts with "$", throw away the "$" and return

WARNING: rapidly written pseudocode resembling python

if type(with\_items\_whatever) == string:

&nbsp;&nbsp;&nbsp;# TODO: throw away $ and get the varname  
&nbsp;&nbsp;&nbsp;items = vars\_or\_whatever\_the\_data\_structure\_is.get(varname, None)  
&nbsp;&nbsp;&nbsp;if items is None:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise AnsibleError("…")  
&nbsp;&nbsp;&nbsp;if type(items) != list:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise AnsibleError("…")

else if type(with\_items\_whatever) == list:

&nbsp;&nbsp;&nbsp;# what it does now

--Michael

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [May 2, 2012, 2:37am UTC](https://forum.ansible.com/t/with-items-from-var/11485/9 "2012-05-02T02:37:26Z")

</div>

OK, I was wanting to pass in some extra-vars (comma delimited), but this will work fine, as I can get my inventory script to massage the data into a list.
