# Doc example of using list variable with with\_items?

**URL:** <https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682>\
**Category:** Ansible Project\
**Created:** [July 14, 2012, 6:41pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682 "2012-07-14T18:41:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [July 14, 2012, 6:41pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/1 "2012-07-14T18:41:30Z")

</div>

As one of the steps in a provisioning playbook, I want to have a host-specific list of data directories that are copied from a backup to the host being provisioned. One possible approach to doing that would be to have my inventory (“hosts”) file contain a host-specific list of directory names to populate for each host, and then iterate over that list using with\_items in a task.

It sounds from the discussion here:

[https://groups.google.com/forum/?fromgroups#!topic/ansible-project/h4bx0Mw6Pfw](https://groups.google.com/forum/?fromgroups#!topic/ansible-project/h4bx0Mw6Pfw)

…and the issue here:

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

…like it should be possible in current ansible for me to define a list variable in my inventory file, then have:

with\_items: $list\_var

…in my task. But I don’t see an example of using a list variable with with\_items in the docs. Have I missed that? If it’s currently missing from the documentation, could it be added?

---

<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 14, 2012, 6:43pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/2 "2012-07-14T18:43:41Z")

</div>

"with\_items: $list" should be added to the docs about with\_items, yes, this has been a feature for a while.

Send me a pull request to update the rst/ file in the doc project.

---

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [July 14, 2012, 7:13pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/3 "2012-07-14T19:13:18Z")

</div>

I’m not sure how to define the list variable in my inventory file if I’m not using the YAML-format inventory file. If I were using YAML format, I assume I could do something like this:

- host: saturn  
vars:
- moons:
- titan
- iapetus
- dione

…and then access the list in my task like:

with\_items: $moons

but in the non-YAML hosts file, I’m not sure how to define a list variable. Let’s say I start like this:

[saturnhosts]  
saturn

…then might I do this?

[saturnhosts:vars]  
moons=titan  
moons=iapetus  
moons=dione

…or maybe this:

[saturnhosts:vars]  
moons=titan iapetus dione

…or something like this:

[saturnhosts:vars]  
moons=  
titan  
iapetus  
dione

---

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [July 14, 2012, 8:44pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/4 "2012-07-14T20:44:06Z")

</div>

After further poking around, I’m now suspicious that it’s not possible to define a list variable using the INI-style inventory file, though it looks like I can define one if I use the YAML-style inventory file. It seems like an unfortunate trap for the unwary that the INI-style inventory file (which I like for the way it makes it easy to define groups and group variables) would have the limitation that it doesn’t allow list variables.

I may be able to accomplish my larger goal by defining my host-specific list-variables in a file imported with vars\_file. So, with hosts named after planets, could I perhaps do something like this?

---

<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 14, 2012, 8:58pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/5 "2012-07-14T20:58:15Z")

</div>

You can't

-- Michael

---

<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 14, 2012, 9:00pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/6 "2012-07-14T21:00:29Z")

</div>

with\_items cannot use host specific vars at this time as all hosts get the same tasks. Use yaml and inventory vars.

-- Michael

---

<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 15, 2012, 12:01am UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/7 "2012-07-15T00:01:27Z")

</div>

In case you didn't infer from my other note, I just fixed this about  
not being able to use vars\_files + with\_items like I just said you  
couldn't.

(I have no plan to make it so you can define lists or hashes in the  
INI file though)

(Last email for today, I promise!)

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [July 15, 2012, 9:33am UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/8 "2012-07-15T09:33:41Z")

</div>

As a matter of completeness, the INI file format allows to make a group of groups, which the YAML syntax doesn't do (yet). We should probably add code for this:

- group: animals  
&nbsp;&nbsp;&nbsp;groups: [cat, dog, mouse]

which is the same as:

- group: animals  
&nbsp;&nbsp;&nbsp;groups:  
&nbsp;&nbsp;&nbsp;- cat  
&nbsp;&nbsp;&nbsp;- dog  
&nbsp;&nbsp;&nbsp;- mouse

I added this to the issue-tracker as: [https://github.com/ansible/ansible/issues/594](https://github.com/ansible/ansible/issues/594)

---

<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 15, 2012, 12:16pm UTC](https://forum.ansible.com/t/doc-example-of-using-list-variable-with-with-items/11682/9 "2012-07-15T12:16:15Z")

</div>

> As a matter of completeness, the INI file format allows to make a group  
> of groups, which the YAML syntax doesn't do (yet).

+1, would like to see someone add this.
