# with\_items in playbook context

**URL:** <https://forum.ansible.com/t/with-items-in-playbook-context/12799>\
**Category:** Ansible Project\
**Created:** [May 13, 2013, 2:46pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799 "2013-05-13T14:46:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastermind](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mastermind](https://forum.ansible.com/u/mastermind)\
**Post date:** [May 13, 2013, 2:46pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/1 "2013-05-13T14:46:50Z")

</div>

Hi,

ive got the following playbook:

[https://gist.github.com/anonymous/ecb41b6fa411871fb69c](https://gist.github.com/anonymous/ecb41b6fa411871fb69c)

with the following var\_files:

[https://gist.github.com/anonymous/6d33670bf74388dd3d39](https://gist.github.com/anonymous/6d33670bf74388dd3d39)

But its not possible to iterate over with\_items in playbook context. But how can i do checks in the next task (probe if tomcat-manager call was successful) with registered variable (tomcat\_response)  
if the iterate only possible in the first task… Maybe theres a better way to do things like this but … I’m really puzzled atm…

Thanks for help!

Lasse

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 13, 2013, 2:57pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/2 "2013-05-13T14:57:16Z")

</div>

​use with\_items on the first task, register the response, then iterate over  
the results of that response

command: ... ${item.content}...  
with\_items: ${tomcat\_response.results}​

(I might have the specific subkeys wrong, but you get the idea)

Serge

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [May 13, 2013, 10:57pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/3 "2013-05-13T22:57:16Z")

</div>

Here’s and example where I do something similar. Took me a while to get the subkeys right.

- name: setup application instance configuration file  
template: \>  
src=dealmax\_app.pl.j2  
dest=/var/www/dealmax/{{ item.application }}/dealmax\_{{ item.application }}\_{{ [item.name](http://item.name) }}.pl  
with\_items: instances  
register: config

- name: restart apps on configuration change  
tags: config  
service: \>  
name=dealmax-{{ item.item.application }}-{{ [item.item.name](http://item.item.name) }}  
state=restarted  
when: item.changed and item.item.enabled

# config.results is a list of all the results of the

# previous task loop including the ‘item’ data passed to the task

with\_items: config.results

---

<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:** [May 13, 2013, 11:25pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/4 "2013-05-13T23:25:57Z")

</div>

FYI: this requires even less boilerplate in 1.2 syntax, just

---

<div class="post-metadata">

**Author:** ![mastermind](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mastermind](https://forum.ansible.com/u/mastermind)\
**Post date:** [May 14, 2013, 8:35am UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/5 "2013-05-14T08:35:19Z")

</div>

Hi,

thanks to all for the hints… this helps me out, but one problem suddenly occured:

[https://gist.github.com/anonymous/e7ca3b6e44a25b7cd466](https://gist.github.com/anonymous/e7ca3b6e44a25b7cd466)

The $childs var doenst resolve anymore the request on localhost look like → GET /manager/undeploy?path=/${childs}

But the var file got childs:

```auto

childs: [!!python/unicode 'myapplication-5.1.0', !!python/unicode 'myapplication-3.1.0']

Once again confused n thankful 4 help...

Lasse

```

---

<div class="post-metadata">

**Author:** ![mastermind](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mastermind](https://forum.ansible.com/u/mastermind)\
**Post date:** [May 14, 2013, 9:30am UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/6 "2013-05-14T09:30:55Z")

</div>

Ok, ive found it:

i tried to load

```auto
${ansible_facts.ansible_fqdn}

but it should be

${ansible_fqdn}

but from me opinion it should be raise an error like 

ERROR: file not found: /tmp/.yml

or something like that, same error if the vars_files are hardcoded and the file doesnt exists, instead of 
running further and resolve no vars..

Greetings 

Lasse

```

---

<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:** [May 14, 2013, 12:14pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/7 "2013-05-14T12:14:41Z")

</div>

I don’t see where “childs” is being set above.

---

<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:** [May 14, 2013, 12:17pm UTC](https://forum.ansible.com/t/with-items-in-playbook-context/12799/8 "2013-05-14T12:17:22Z")

</div>

If vars\_files contains fact variables they are resolved later rather than up front.

It is good form to supply a default in this case, by doing this:

vars\_files:

- [“${somefact}\_foo.yml”, “default.yml”]

I would tend to agree that if you have the bare:

vars\_files:

- “${somefact}.yml”

Should probably produce a host-specific error and fail the task(s).

Note that ${foo} is 1.1 syntax, and in 1.2, we still support that but far {{ foo }}
