Task vars + Jinja template

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:

- 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:

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



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

FCC:      14624

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

But, if the count is 0, it doesn't fall into the 'else' condition.
FCC:      0
<!-- inside queue greater than 1 -->





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

Thanks!

Try casting it explicitly to int?

Hi Dick -

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

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

Is there another way?


I just added debug to the variable:

<!-- 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?

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:

{% if foundQueuesCount|int > 0 %}


Thanks, Dick!