# Working with with\_items and some filter

**URL:** <https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027>\
**Category:** Ansible Project\
**Created:** [August 25, 2014, 8:46am UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027 "2014-08-25T08:46:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bastian\_Bringenberg](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Bastian\_Bringenberg](https://forum.ansible.com/u/Bastian_Bringenberg)\
**Post date:** [August 25, 2014, 8:46am UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/1 "2014-08-25T08:46:30Z")

</div>

Hey ho,

I would like to deploy an application to several vhosts is item.useApplication exists. I am possible to do that, download files and so on and so on. But I get a problem as soon as I want to do an action if the application is never installed.

Normally:

> - stat: path=/etc/.git
> 
> register: exists\_git
> 
> ignore\_errors: yes
> 
> - lineinfile: dest=/etc/rkhunter.conf line=‘ALLOWHIDDENDIR=“/etc/.git”’ owner=root group=root mode=0644
> 
> when: exists\_git.stat.exists

But how is it possible for me to do this combined with “with\_items”? One approach, that (untested) will not work in my opinion:

> - stat: path=/etc/.git  
> register: exists\_git\_{{ item.name }}  
> with\_items: hosts  
> when: item.useApplication  
> ignore\_errors: yes
> 
> - lineinfile: dest=/etc/rkhunter.conf line=‘ALLOWHIDDENDIR=“/etc/.git”’ owner=root group=root mode=0644  
> with\_items: hosts  
> when: item.useApplication  
> when: exists\_git\_{{ item.name }}.stat.exists

Is someone able to help me here?

Have a nice week,  
Greetings from Bochum, RuhrArea, Germany,

Bastian

PS: rkhunter is just a demonstration how I do this normally^^.

---

<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:** [August 25, 2014, 2:06pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/2 "2014-08-25T14:06:28Z")

</div>

You can’t say “when:” twice, but you can feed it a list.

when:

- item.useApplication
- other\_conditional

What you are doing with building a variable name in a conditional is a little weird, and this doesn’t work.

Rather, register when used with “with\_items” will return an array, which you can easily debug:

- stat: path=/etc/.git  
register: stats  
with\_items: hosts  
when: item.useApplication

- debug: var=stats

Though ultimately, it looks like you should drop the lineinfile and just include your logic in a template, and it will be much cleaner and easier.

---

<div class="post-metadata">

**Author:** ![Bastian\_Bringenberg](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Bastian\_Bringenberg](https://forum.ansible.com/u/Bastian_Bringenberg)\
**Post date:** [August 25, 2014, 2:55pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/3 "2014-08-25T14:55:45Z")

</div>

Hey ho,

> You can’t say “when:” twice, but you can feed it a list.
> 
> when:
> 
> - item.useApplication
> - other\_conditional

perfect!

> What you are doing with building a variable name in a conditional is a little weird, and this doesn’t work.
> 
> Rather, register when used with “with\_items” will return an array, which you can easily debug:
> 
> - stat: path=/etc/.git  
> register: stats  
> with\_items: hosts  
> when: item.useApplication
> 
> - debug: var=stats
> 
> Though ultimately, it looks like you should drop the lineinfile and just include your logic in a template, and it will be much cleaner and easier.

I do that with templates but I want to unzip, move and copy some files to create a perfect application environment. Perhaps your return of an array will solve my problem^^.

As soon as I tried this solution I will give you an answer! Thanks for the moment!

---

<div class="post-metadata">

**Author:** ![Bastian\_Bringenberg](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Bastian\_Bringenberg](https://forum.ansible.com/u/Bastian_Bringenberg)\
**Post date:** [August 25, 2014, 6:58pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/4 "2014-08-25T18:58:39Z")

</div>

I am not able to figure out which array position is important for the current item as:

hosts:

- 1 - noApplication ( ID = 3 )
- 2 - Application ( ID = 2 )
- 3 - Application ( ID = 42 )

would be

- 1 ( ID = 2 )
- 2 ( ID = 42 )

And I see no way to match those… Any Idea? Or is it possible without a lot of problems to create a plugin in ansible to solve this 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:** [August 25, 2014, 10:26pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/5 "2014-08-25T22:26:21Z")

</div>

Sorry, I’m no following the above.

Can we step back a moment?

This appears to be a playbook that fixes various things in particular git directories, if found, for a very large number of git directories?

Knowing what you are trying to do might help suggest a more idiomatic solution.

---

<div class="post-metadata">

**Author:** ![Bastian\_Bringenberg](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Bastian\_Bringenberg](https://forum.ansible.com/u/Bastian_Bringenberg)\
**Post date:** [August 26, 2014, 6:17am UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/6 "2014-08-26T06:17:47Z")

</div>

Okay =).

I could write it down in some kind of PHP so you get my point.

I have got:

hosts:

- name: “domain.tld”  
database:  
name: test  
user: test  
pw: test  
systemUser: domain  
useApplication: true

This will create me /var/www/domain.tld with everything I normally need. No I add “useApplication” for example ownCloud.  
The steps I need to do are:

- Download Zip File
- Extract Zip File
- Move Content of ZipFile Around ( important as owncloud.zip contains a folder named owncloud/ )
- Install a Template for the Settings
- Run a Database Dummy
- Remove WebDav
- Restart Apache

But only do that if there is no locking file. I do not want to have all of this done on each run and need some kind of if( ! file\_exists(lockfile) ) doEverything();  
And all of this should of course only happen if useApplication is set to true^^.

foreach($hosts as $item) {  
if( $item-\>useApplication ) {  
if( ! file\_exsists( $item-\>getLockfile() ) ) {  
doAllAbove();  
}  
}  
}

Is this more clear?

---

<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:** [August 26, 2014, 12:42pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/7 "2014-08-26T12:42:54Z")

</div>

Not really.

Is the issue you have a delegated host action that you only want to run once?

If so you could factor this out to an action that happens last in the playbook.

---

<div class="post-metadata">

**Author:** ![Bastian\_Bringenberg](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Bastian\_Bringenberg](https://forum.ansible.com/u/Bastian_Bringenberg)\
**Post date:** [August 26, 2014, 10:14pm UTC](https://forum.ansible.com/t/working-with-with-items-and-some-filter/16027/8 "2014-08-26T22:14:19Z")

</div>

Not only once a run, I want to have it run only to install it^^.

So if the vhost Application is already installed I don’t want to have the package to unzip anymore, move files around or run the database dummy. I am able to control this behavior without “with\_items” but as soon as I a have an array of informations to be checked for each I am not able to

> 

Another option would be to register the stats informations into my “item” from the config array so I am able to say:

- name: whatever  
whatever command  
with\_items: hosts  
when: item.myFile.stats.exists

Easy what I need to do:
