# Multiple items and a long list of conditional tasks

**URL:** <https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514>\
**Category:** Ansible Project\
**Created:** [June 20, 2014, 11:50am UTC](https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514 "2014-06-20T11:50:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Cannaerts](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Tom\_Cannaerts](https://forum.ansible.com/u/Tom_Cannaerts)\
**Post date:** [June 20, 2014, 11:50am UTC](https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514/1 "2014-06-20T11:50:03Z")

</div>

I’m writing a playbook to create a number of solr cores, but I’m running into some issues.

I started with a task file that would be included, but appearantly include and with\_items isn’t supported.

The idea was, that for every core I would check that the base folder of the core existed. This result (stat) would be saved to a variable.

- name: check for core existence

stat: path=/usr/share/solr/live/solr/cores/{{ item.name }}  
register: st\_cores  
with\_items: solr\_cores

Then, to enumerate over it in later tasks, I could use the with\_indexed\_items and use a conditional when to exclude the cores that were already present  
eg:

- name: Copy example core  
shell: cp -R /usr/share/solr/example/solr/collection1/conf/\* /usr/share/solr/live/solr/cores/{{ item.0.name }}/conf/  
with\_items: solr\_cores

when: not st\_cores.results[{{ item.0 }}].stat.exists

This works fine for most of the tasks, but I also have a number of tasks that use with\_items themselves. I managed to combine them using with\_nested, but I can’t figure out how to include the conditional, as I don’t have the index as with with\_indexed\_items

eg.

- name: Copy some files  
shell: cp /some/folder/{{ item[1] }} /usr/share/solr/cores/{{ item[0].name }}/  
with\_nested:
- solr\_cores
- [‘somefile.xml’, ‘anotherfile.xml’, ‘evenmorefiles.xml’]  
when: not st\_cores.results[{{ item[0].0 }}].stat.exists

Basically, I can’t check stat results, as I don’t know the index of the current item in that array. Any ideas on how I can solve this? Either I need a way to be able to check for the file existance on some other way that does work with with\_nested, or I need some sort of creative workaround for the include with\_items problem.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 20, 2014, 9:03pm UTC](https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514/2 "2014-06-20T21:03:28Z")

</div>

Might it be easier to write a simple fact module that returns what cores you have, rather than doing the register?

This way you could return a hash table indexed by core name, as well as a list of cores, which might make the playbooks more intuitive and less relying on some rather “programmy” concepts that Ansible usually tries to avoid.

---

<div class="post-metadata">

**Author:** ![Dan\_Swartz](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dan\_Swartz](https://forum.ansible.com/u/Dan_Swartz)\
**Post date:** [July 10, 2015, 3:41pm UTC](https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514/3 "2015-07-10T15:41:20Z")

</div>

Of course! I keep forgetting that the “programmy” stuff should go in Python 🙂  
When you talk about a ‘fact’ module, though, what do you mean? Writing a module that takes in a bunch of data formatted in some way and then exit\_json( [changed=False,] yourStuff=yourStuff ), where “yourStuff” is a dictionary/object/etc that you can then register and iterate over the results? Or, is there some sort of API method where modules can set facts themselves? I ask because I have a similar task, in that I need to read in a file and then perform a set of tasks based on the results.

For example, if my file “foo.xml” looks like:

`<foos> <foo bar="a"> <baz Name="a1"> <baz Name="a2"> </foo> <foo bar="b"> <baz Name="b1"> <baz Name="b2"> <baz Name="b3"> </foo> </foos>`

Then, for each foo, I will have to do one set of tasks to handle the bar value and then another set of tasks to handle each bar’s baz. I am wondering how I can accomplish this while operating within the Ansible paradigm. Can you elaborate a little more on how this could/should be accomplished? Thanks!

---

<div class="post-metadata">

**Author:** ![Dan\_Swartz](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dan\_Swartz](https://forum.ansible.com/u/Dan_Swartz)\
**Post date:** [July 10, 2015, 3:44pm UTC](https://forum.ansible.com/t/multiple-items-and-a-long-list-of-conditional-tasks/15514/4 "2015-07-10T15:44:10Z")

</div>

Is this what “block” tasks are meant to handle?
