# Task vars + Jinja template

**URL:** <https://forum.ansible.com/t/task-vars-jinja-template/30858>\
**Category:** Ansible Project\
**Created:** [July 24, 2019, 1:14pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858 "2019-07-24T13:14:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [July 24, 2019, 1:14pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858/1 "2019-07-24T13:14:49Z")

</div>

Hi -

I’m trying to evaluate the length of “foundQueuesCount” below in a Jinja template and use a conditional. Everything works perfectly except for when the length is zero.

The task:

```auto
- name: build routerconfig
  template:
    src: templates/{{item.version}}/routerconfig.j2
    dest: "{{smq_path}}/config/{{item.router_name}}/routerconfig.xml"
    owner: appuser
    group: appuser
    mode: 0744
  with_items: "{{routers}}"
  vars:
    foundQueues: "{{lookup('file', '{{appdataRoot}}/tempqueues_{{item.router_name}}.txt', wantlist=True) }}"
    foundQueuesCount: "{{lookup('file', '{{appdataRoot}}/tempqueues_{{item.router_name}}.txt')|length|int }}"
  when: item.hostname == ansible_hostname

```

From the template:

```auto
FCC: {{ foundQueuesCount }}
{% if foundQueuesCount > 1 %}
<!-- inside queue greater than 1 -->
{% else %}
<!-- inside else condition -->
{% endif %}

```

```auto

```

```auto

```

```auto
It finds the count correctly when the temp file has records & goes into the correct part of the 'if' condition:

```

```auto

FCC: 14624

<!-- inside queue greater than 1 -->

```

```auto
But, if the count is 0, it doesn't fall into the 'else' condition.

```

```auto
FCC: 0
<!-- inside queue greater than 1 -->

```

```auto

```

```auto

```

```auto

```

```auto

```

```auto
This is really simple. What in the world am I overlooking?

```

```auto

```

```auto
Thanks!

```

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 24, 2019, 1:25pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858/2 "2019-07-24T13:25:19Z")

</div>

Try casting it explicitly to int?

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [July 24, 2019, 1:40pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858/3 "2019-07-24T13:40:53Z")

</div>

Hi Dick -

Isn’t that done with |int? like this:

```auto
foundQueuesCount: "{{lookup('file', '{{appdataRoot}}/tempqueues_{{item.router_name}}.txt')|length|int }}"

```

```auto

```

```auto
Is there another way?

```

```auto

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [July 24, 2019, 1:48pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858/4 "2019-07-24T13:48:24Z")

</div>

I just added debug to the variable:

```auto
<!-- FCC: {{ foundQueuesCount }}
 {{ foundQueuesCount | type_debug }} -->

```

and got this:

Are all variables passed to by Ansible to Jinja as text? If so, I guess I need to cast them to their proper type within the Jinja template?

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [July 24, 2019, 2:02pm UTC](https://forum.ansible.com/t/task-vars-jinja-template/30858/5 "2019-07-24T14:02:17Z")

</div>

I guess that’s it. I have to cast the variable to the desired type in the Jinja template. I changed it to this & it works:

```auto
{% if foundQueuesCount|int > 0 %}

```

```auto

```

```auto
Thanks, Dick!

```
