# looping over variables

**URL:** <https://forum.ansible.com/t/looping-over-variables/19903>\
**Category:** Ansible Project\
**Created:** [November 30, 2015, 9:49pm UTC](https://forum.ansible.com/t/looping-over-variables/19903 "2015-11-30T21:49:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saturb](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Saturb](https://forum.ansible.com/u/Saturb)\
**Post date:** [November 30, 2015, 9:49pm UTC](https://forum.ansible.com/t/looping-over-variables/19903/1 "2015-11-30T21:49:35Z")

</div>

hello everyone, need some help here.

i have this variable;

vars:  
ufw\_applications:

- { name: sample\_app, ports: [{ port: 8081, protocol: tcp }, { port: 8082, protocol: udp }] }

and i want to have a resulting config as;

[app2]  
title=“sample\_app”  
ports=8081/tcp|8082/udp

my template looks like this but is not working. any help is appreciated.

[{{ item.name }}]  
title=“{{ item.name }}”  
{% if item.port is defined and item.protocol is defined %}  
ports={{ item.port }}/{{ item.protocol }}  
{% endif %}  
{% if item.port is defined and item.protocol is not defined %}  
ports={{ item.port }}/tcp|{{ item.port }}/udp  
{% endif %}  
{% if item.ports is defined %}  
ports={{ {{ item.ports.port }}/{{ item.ports.protocol }}|join(‘|’) }}  
{% endif %}
