# Sorting a fact in Ansible

**URL:** <https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899>\
**Category:** Ansible Project\
**Created:** [October 19, 2021, 5:29pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899 "2021-10-19T17:29:09Z")\
**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:** [October 19, 2021, 5:29pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899/1 "2021-10-19T17:29:09Z")

</div>

I am trying to sort a list when setting a fact in Ansible as follows:

- name: Get installed packages  
become: true  
become\_method: sudo  
package\_facts:  
register: installed

- name: Set package facts  
set\_fact:  
packages: “{{ ansible\_facts.packages|dict2items|sort }}”

This works when I run it from the command line, but when I do this within Ansible Tower, I get the following error:

Unexpected templating type error occurred on ({{ ansible\_facts.packages|dict2items|sort }}): ‘\<’ not supported between instances of ‘dict’ and ‘dict’

I know this isn’t a Tower list, but would anyone have any idea why this would work in the Ansible CLI but not in Tower?

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:** [October 19, 2021, 5:31pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899/2 "2021-10-19T17:31:50Z")

</div>

I’d recommend that you use `dictsort` instead of `dict2items` It achieves the goal very similarly, but sorts by default, and would avoid the error you are getting.

The biggest difference is instead of having `item.key` and `item.value`, you have `item.0` and `item.1`.

[https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.dictsort](https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.dictsort)

---

<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:** [October 19, 2021, 6:02pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899/3 "2021-10-19T18:02:19Z")

</div>

So if I set the fact as follows:

- name: Set package facts  
set\_fact:  
packages: “{{ ansible\_facts.packages|dictsort }}”

How would I traverse that in my template?:

{% for v in hostvars[i][‘packages’] %}  
{% for pkg in v.value %}  
Package: {{ pkg.name }} Version: {{ pkg.version}} Arch: {{ pkg.arch }} Release: {{ pkg.release }}  
{% endfor %}  
{% endfor %}

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:** [October 19, 2021, 6:21pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899/4 "2021-10-19T18:21:23Z")

</div>

from a quick glance, it looks like you would change `v.value` to `v.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:** [October 19, 2021, 6:25pm UTC](https://forum.ansible.com/t/sorting-a-fact-in-ansible/35899/5 "2021-10-19T18:25:35Z")

</div>

OK, that took care of it. Worked great! I appreciate the timely response!

Thanks,  
Harry
