# with\_items combined with with\_first\_file ?

**URL:** <https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329>\
**Category:** Ansible Project\
**Created:** [January 13, 2014, 1:18pm UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329 "2014-01-13T13:18:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@martin1](https://forum.ansible.com/u/martin1)\
**Post date:** [January 13, 2014, 1:18pm UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/1 "2014-01-13T13:18:37Z")

</div>

I’ve been looking for a solutions for this, but I can’t seem to figure it out. Here is what I want to do:

I’m writing a playbook to manage a number of DNS servers. I have a list of zones in my vars file, like so:

list\_of\_zones:

- [a.com](http://a.com)
- [b.com](http://b.com)
- [c.com](http://c.com)  
etc…

I then have a dir with each zone file in it:

zones/a.com.db  
zones/b.com.db  
etc…

However, for some of the zones, I just want to use a default zone file. So basically, what I would like to do is something like this:

- name: Copy zone files  
copy: src=zones/{{ item }} dest=/var/named/zones/{{ item }}  
with\_first\_file:
- path/to/zones/{{ item }}.db
- path/to/zones/default.db  
with\_items:
- list\_of\_zones

This however does not work.

I tried to split out the copy part and with\_first\_file to a different file in the playbook and include it, passing the zone as a var, but I then got a warning that using with\_items together with include was not recommended and that it was going to be deprecated.

I’m a bit lost how to solve this. One option would of course be to extend my zones var to include the name of the zone file as well, but I would like to make this as dynamic as possible. Any ideas are welcome.

Thanks,  
/Martin

---

<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:** [January 13, 2014, 9:53pm UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/2 "2014-01-13T21:53:25Z")

</div>

Something like this might work:  
​​  
- name: Copy zone files  
&nbsp;&nbsp;copy: src={{  
​lookup('first\_found', ['path/to/zones/' + ​  
item  
​  
+ '.db'  
​  
​, 'path/to/zones/default + '.db'])​  
}} dest=/var/named/zones/{{ item }}  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- list\_of\_zones

​Serge​

---

<div class="post-metadata">

**Author:** ![martin1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@martin1](https://forum.ansible.com/u/martin1)\
**Post date:** [January 14, 2014, 8:34am UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/3 "2014-01-14T08:34:37Z")

</div>

Hi Serge,

This worked great. I didn’t realize that you could use the with\_\* functions with lookup(), now things makes more sense. Thank you!

Best regards,  
/Martin

---

<div class="post-metadata">

**Author:** ![martin1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@martin1](https://forum.ansible.com/u/martin1)\
**Post date:** [January 14, 2014, 8:37am UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/4 "2014-01-14T08:37:00Z")

</div>

Hi Serge,

This worked great, thank you. I didn’t realize that you could use the with\_\* functions with lookup(), now things makes a lot more sense.

Best regards,  
/Martin

---

<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:** [January 14, 2014, 3:16pm UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/5 "2014-01-14T15:16:01Z")

</div>

It’s not really intended that first\_found be used in this way, because it’s a plugin that returns lists to iterate on, as opposed to ‘file’.

However, it is definitely something that will continue to work.

It just wouldn’t make sense using with\_items with, say, ‘items’ in the lookup, for that, go to things like ‘with\_nested’.

---

<div class="post-metadata">

**Author:** ![Klokkie](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@Klokkie](https://forum.ansible.com/u/Klokkie)\
**Post date:** [July 25, 2014, 6:54am UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/6 "2014-07-25T06:54:53Z")

</div>

Just out of curiosity, if not with lookup(‘first\_found’,…) how would you do

…  
with\_items: itemlistvar  
with\_first\_found:

- file1
- file2
- etc…

Are you suggesting:  
with\_nested:

- itemlistvar
- lookup(‘first\_found’, … )

---

<div class="post-metadata">

**Author:** ![Klokkie](https://avatars.discourse-cdn.com/v4/letter/k/ed655f/32.png) [@Klokkie](https://forum.ansible.com/u/Klokkie)\
**Post date:** [July 25, 2014, 7:56am UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/7 "2014-07-25T07:56:51Z")

</div>

I found that the inline lookup "src={{  
​lookup(‘first\_found’, [‘path/to/zones/’ + ​  
item  
​

- ‘.db’  
​  
​, 'path/to/zones/default + ‘.db’])​  
}}" did not quite work for me.  
The following example did the trick (for me) tho.

As an example:

---

<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:** [July 25, 2014, 7:20pm UTC](https://forum.ansible.com/t/with-items-combined-with-with-first-file/14329/8 "2014-07-25T19:20:20Z")

</div>

When you say something doesn’t work for you, please describe HOW it doesn’t work.

It’s unclear if you hit a traceback that we need to deal with or what.

But no, with\_first\_found is designed to implement inline loops, not to be used like that.

Some lookup plugins are for simple strings, others provide iterators.

I agree this is somewhat confusing but that’s why we show them seperately in the docs with appropriate-ish examples for each.
