# Is it possible to use jinja2 templates in tasks?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-use-jinja2-templates-in-tasks/19860>\
**Category:** Ansible Project\
**Created:** [November 26, 2015, 5:06am UTC](https://forum.ansible.com/t/is-it-possible-to-use-jinja2-templates-in-tasks/19860 "2015-11-26T05:06:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Artemenko](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@Alexander\_Artemenko](https://forum.ansible.com/u/Alexander_Artemenko)\
**Post date:** [November 26, 2015, 5:06am UTC](https://forum.ansible.com/t/is-it-possible-to-use-jinja2-templates-in-tasks/19860/1 "2015-11-26T05:06:49Z")

</div>

Hi! I’m trying to implement firewall rules management with `ufw` module. Idea is to have list of ports to be accessable from sources somewhere in host variables. For example, I have this in my `group_vars/consul_servers`:

```auto
ufw_open_docker_from_sources:
  - {port: 8500, src: consul_agents}
  - {port: 8301, src: consul_agents}

```

Where `consul_agents` is a group name.

And I have a `firewall/main.yml` in my `rules`, where I’m trying to expand this list of ports and groups into some tasks:

```auto
{% for item in hostvars['ufw_open_docker_from_sources'] %}
{% for host in groups[item.src] %}
- name: Enable access to some docker ports from particular source
  ufw:
    insert=1
    route=yes
    rule=allow
    port={{ item.port }}
    src={{ hostvars[host]['ip'] }}
{% endfor %}
{% endfor %}

```

But this does not work because of error:

```auto
ERROR! Syntax Error while loading YAML.

The error appears to have been in '/Users/art/projects/ansible/with-roles/roles/firewall/tasks/main.yml': line 25, column 2, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

{% for item in hostvars['ufw_open_docker_from_sources'] %}
 ^ here

```

Is it possible to overcome this limitation?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 26, 2015, 6:19am UTC](https://forum.ansible.com/t/is-it-possible-to-use-jinja2-templates-in-tasks/19860/2 "2015-11-26T06:19:01Z")

</div>

no, you cannot use jinja to generate tasks on the fly.

you should look into the different lookups as you can combine them to  
create what you need in a single task.

---

<div class="post-metadata">

**Author:** ![Alexander\_Artemenko](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@Alexander\_Artemenko](https://forum.ansible.com/u/Alexander_Artemenko)\
**Post date:** [November 27, 2015, 7:52am UTC](https://forum.ansible.com/t/is-it-possible-to-use-jinja2-templates-in-tasks/19860/3 "2015-11-27T07:52:09Z")

</div>

I see. I found a workaround using `with_subelements`:

Now I have:

`  
ufw\_open\_docker\_from\_sources:

- {port: 8500, src: “{{ groups.all\_consuls }}”}
- {port: 8301, src: “{{ groups.all\_consuls }}”}  
`

in my group\_vars file. This way, I have all hosts from the group as a list in my `src` attribute.

And this piece in my tasks:

`

- name: Enable access to some docker ports from particular source  
ufw:  
insert=1  
route=yes  
rule=allow  
port={{ item.0.port }}  
src={{ hostvars[item.1][‘ip’] }}  
with\_subelements:
- ufw\_open\_docker\_from\_sources
- src

`

This generates a one rule for each host, as desired.
