Doc example of using list variable with with_items?

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

…and the issue here:

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?

"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.

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

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?

You can't

-- Michael

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

-- Michael

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!)

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
   groups: [ cat, dog, mouse ]

which is the same as:

- group: animals
   groups:
   - cat
   - dog
   - mouse

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

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.