# Using fact as loop control in Jinja2 template

**URL:** <https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239>\
**Category:** Ansible Project\
**Created:** [June 23, 2020, 6:52pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239 "2020-06-23T18:52:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [June 23, 2020, 6:52pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239/1 "2020-06-23T18:52:38Z")

</div>

I have a playbook that will use the user\_find API call against our FreeIPA server to retrieve a list of all users. What I’m trying to do is get the total count, then use that count in my j2 file. I’m getting the count as follows:

- name: Set IDM facts  
set\_fact:  
idmcount: “{{ userfind.json.result.count|int }}”

- name: Output data  
template:  
src: uid.csv.j2  
dest: uid.csv

In my j2 file, I’ve tried 2 different things:

Option 1:

{% for i in idmcount %}  
{{ i }}  
{% endfor %}

This prints the count as strings (the value is 1740):  
1  
7  
4  
0

Option 2:

{% for i in {{ idmcount }} %}  
{{ i }}  
{% endfor %}

This gives me the following error:

fatal: [localhost]: FAILED! =\> {“changed”: false, “msg”: “AnsibleError: template error while templating string: expected token ‘:’, got ‘}’. String: {% for i in {{ idmcount }} %}\n{{ i }}\n{% endfor %}\n”}

So how do I use that count as a loop control variable?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 23, 2020, 7:22pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239/2 "2020-06-23T19:22:04Z")

</div>

Do you want to loop over a range of numbers up to idmcount? I think that is what I am reading, in which case you want:

{% for i in range(1, idmcount|int + 1) %}

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [June 23, 2020, 7:31pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239/3 "2020-06-23T19:31:44Z")

</div>

Thank you! That worked well. I must’ve really been over thinking it. However, this does lead to another question: now I’m putting in the actual content as follows:

{% for i in range(1, idmcount|int +1) %}  
{{ users[i].uid }},{{ users[i].uidnumber }}  
{% endfor %}

This works fine, but the values appear to be in (what I think) is the json format. For example:

[u’name1’],[u’1111’]

How do I strip out the “u’” and have just:  
name,1111

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 23, 2020, 8:04pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239/4 "2020-06-23T20:04:03Z")

</div>

It sounds like you have a data structure like this:

users

- uid:
- name1  
uidnumber:
- 1111

In which case I’d recommend:

users

- uid: name1  
uidnumber: 1111

Otherwise, You would need to use something like: {{ users[i].uid[0] }}

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [June 23, 2020, 8:13pm UTC](https://forum.ansible.com/t/using-fact-as-loop-control-in-jinja2-template/33239/5 "2020-06-23T20:13:49Z")

</div>

Perfect!!! Exactly what I needed.

Thank you very much!  
Harry
