# Use items from task condition in template

**URL:** <https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037>\
**Category:** Ansible Project\
**Created:** [August 26, 2014, 10:40am UTC](https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037 "2014-08-26T10:40:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergei\_Antipov](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Sergei\_Antipov](https://forum.ansible.com/u/Sergei_Antipov)\
**Post date:** [August 26, 2014, 10:40am UTC](https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037/1 "2014-08-26T10:40:11Z")

</div>

Hello.

Please tell me how I can use items from task condition in template. Or other method, how I can decide my probled.  
goal: I have a template for /etc/network/interfaces file. I want to set up bond interface, which contains the parameter “slaves XXX XXX XXX”, where XXX - name of interface.  
I want use only interfaces, which name match ethX regex, for example “slaves eth0 eth1”.  
How I can do this template with ansible, maybe I should use ansible\_interfaces variable or should use shell for register intefaces names?

---

<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:47pm UTC](https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037/2 "2014-08-26T12:47:18Z")

</div>

I’m not sure what you mean you say “use items from the task condition”, but you can in fact use conditionals with your template.

Take a look at the Jinja2 documentation, and you’ll see things like

{% if x %}

{% else %}

{% endif %}

and these can be put in your .j2 files.

I might have misunderstood the question.

---

<div class="post-metadata">

**Author:** ![Sergei\_Antipov](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Sergei\_Antipov](https://forum.ansible.com/u/Sergei_Antipov)\
**Post date:** [August 27, 2014, 3:23am UTC](https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037/3 "2014-08-27T03:23:40Z")

</div>

example:

- name: create /etc/network/intefaces file  
template: src=xxx dest=xxx  
with\_items: ansible\_interfaces  
when: item | match(“eth\*”)

and in template I need:  
…  
slaves eth0 eth1 eth2 ethX

…

---

<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 27, 2014, 12:21pm UTC](https://forum.ansible.com/t/use-items-from-task-condition-in-template/16037/4 "2014-08-27T12:21:10Z")

</div>

{{ ansible\_interfaces }} will be available in the template and you’ll be able to loop over it in the template logic.

Do not use with\_items on the template task, as this will result in writing the file N different times, and will not do what you want.
