# using 2 loops with\_first\_found and with\_items

**URL:** <https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739>\
**Category:** Ansible Project\
**Created:** [July 23, 2014, 4:24am UTC](https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739 "2014-07-23T04:24:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hudec\_Peter](https://avatars.discourse-cdn.com/v4/letter/h/a87d85/32.png) [@Hudec\_Peter](https://forum.ansible.com/u/Hudec_Peter)\
**Post date:** [July 23, 2014, 4:24am UTC](https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739/1 "2014-07-23T04:24:45Z")

</div>

Hi,

I’m trying to write role for configuration of the package management. In  
this role I following setup.

— defaults/main.yml  
debain\_source\_list:  
&nbsp;&nbsp;- name: debian  
&nbsp;&nbsp;state: enabled  
- name: debian-backports  
&nbsp;&nbsp;state: enabled  
- name: debian-lts  
&nbsp;&nbsp;state: enabled

— task/setup-Debian.yml  
- name: "[repository] apt source list files"  
&nbsp;&nbsp;action: template src=??? dest=/etc/apt/sources.d/???.list  
&nbsp;&nbsp;with\_items: " {{ debain\_source\_list }}"  
&nbsp;&nbsp;when: "item.state != 'enabled'"  
&nbsp;&nbsp;with\_first\_found:  
&nbsp;&nbsp;- {{ item.name }}.list.{{ ansible\_lsb.id }}.{{ ansible\_lsb.codename }}  
&nbsp;&nbsp;- default.list

The idea is to have separate source list files for each Debian release  
/wheeze, squeeze, …/, if the file is not found put there some default  
file. The problem is how to run more loops and address variables in one  
action.

Consider this to be an example and will be used as template for another  
actions. I do not want to sync the directories.

&nbsp;&nbsp;Best regards  
&nbsp;&nbsp;&nbsp;&nbsp;Peter Hudec

---

<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 24, 2014, 11:46pm UTC](https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739/2 "2014-07-24T23:46:53Z")

</div>

Can’t combine these two things.

Also, when using with\_foo: var, don’t use jinja2.

Not like:

with\_items: “{{ foo }}”

Do it like so:

with\_items: alist\_variable

I’d just have a bunch of templates like so:

- template: src={{ item}} dest=/etc/apt/sources.list.d/{{ item.basename }}  
with\_fileglob: ‘\*.sources.j2’

You may need to reference a hash that keys off of something like “{{ item | basename }}” inside your template, such as

{# set basename = item|basename #}  
{{ repo\_url[basename] }}

etc

Sort of make sense?

---

<div class="post-metadata">

**Author:** ![Hudec\_Peter](https://avatars.discourse-cdn.com/v4/letter/h/a87d85/32.png) [@Hudec\_Peter](https://forum.ansible.com/u/Hudec_Peter)\
**Post date:** [July 28, 2014, 1:25pm UTC](https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739/3 "2014-07-28T13:25:12Z")

</div>

Hi Micheal,

I will try your approach but I found it no so pretty as suggested 🙂

Thanks  
Peter

---

<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 28, 2014, 2:13pm UTC](https://forum.ansible.com/t/using-2-loops-with-first-found-and-with-items/15739/4 "2014-07-28T14:13:52Z")

</div>

My suggestion is 2 lines in the playbook versus your 7.
