# State exists for group of items

**URL:** <https://forum.ansible.com/t/state-exists-for-group-of-items/14084>\
**Category:** Ansible Project\
**Created:** [December 6, 2013, 7:12am UTC](https://forum.ansible.com/t/state-exists-for-group-of-items/14084 "2013-12-06T07:12:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Heeney](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@Patrick\_Heeney](https://forum.ansible.com/u/Patrick_Heeney)\
**Post date:** [December 6, 2013, 7:12am UTC](https://forum.ansible.com/t/state-exists-for-group-of-items/14084/1 "2013-12-06T07:12:40Z")

</div>

I keep running into this a lot but I am not sure how to handle it. Lets assume I have the following yaml:

project\_paths:

- path: /srv/www/web/app1  
name: app1
- path: /srv/www/web/app2  
name: app2

Then I want to check if a file exists before running the following action:

- name: application | check for installer  
stat: path={{ item.path }}/install.php  
register: check\_install  
with\_items: project\_paths

- name: application | run install  
command: php install.php chdir={{ item.path }}  
with\_items: project\_paths  
when: check\_install.stat.exists == false

The issue is registering check\_install since we are doing it in a loop. It looks to be resetting the variable for every item. I tried something like this and I get ‘check\_install’ is undefined.

- name: application | check for installer  
stat: path={{ item.path }}/install.php  
register: check\_install[item.name]  
with\_items: project\_paths

- name: application | run install  
command: php install.php chdir={{ item.path }}  
with\_items: project\_paths  
when: check\_install[item.name].stat.exists == false

- name: done  
fail: msg=“{{ item.name } installer does not exist”

with\_items: project\_paths  
when: check\_install[item.name].stat.exists == false

Basically I am wondering the best way to run stat or other register commands with\_items so that I can later check that on other with\_item tasks for the same variables.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 6, 2013, 12:49pm UTC](https://forum.ansible.com/t/state-exists-for-group-of-items/14084/2 "2013-12-06T12:49:54Z")

</div>

Verify the results of a registered variable like so:

- debug: var=register

---

<div class="post-metadata">

**Author:** ![Patrick\_Heeney](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@Patrick\_Heeney](https://forum.ansible.com/u/Patrick_Heeney)\
**Post date:** [December 6, 2013, 5:18pm UTC](https://forum.ansible.com/t/state-exists-for-group-of-items/14084/3 "2013-12-06T17:18:13Z")

</div>

Thank you! This is the code I ended up with for anyone that stumbles upon this:

- debug: \>  
msg=“{{ item.0.path }} - {{ item.1.stat.exists }}”  
with\_together:

- project\_paths

- check\_install.results

- name: application | run install  
command: php install.php chdir={{ item.0.path }}

with\_together:

- project\_paths
- check\_install.results

when: item.1.stat.exists
