# package\_facts module

**URL:** <https://forum.ansible.com/t/package-facts-module/35798>\
**Category:** Ansible Project\
**Created:** [September 29, 2021, 1:56am UTC](https://forum.ansible.com/t/package-facts-module/35798 "2021-09-29T01:56:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Veliss](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@John\_Veliss](https://forum.ansible.com/u/John_Veliss)\
**Post date:** [September 29, 2021, 1:56am UTC](https://forum.ansible.com/t/package-facts-module/35798/1 "2021-09-29T01:56:22Z")

</div>

When using the package\_facts module, I would like to get a listing of the package name and the version.

What I have so far is the below.

- name: Collect Packages installation status  
package\_facts:  
manager: “auto”  
register: packages
- name: debug output  
debug:  
var: ansible\_facts.packages

But this seem to give me the package along with all its details

“yum-utils”: [  
{  
“arch”: “noarch”,  
“epoch”: null,  
“name”: “yum-utils”,  
“release”: “54.el7\_8”,  
“source”: “rpm”,  
“version”: “1.1.31”  
}  
],

I have also tried var: ansible\_facts.packages.version

---

<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:** [September 29, 2021, 8:55am UTC](https://forum.ansible.com/t/package-facts-module/35798/2 "2021-09-29T08:55:06Z")

</div>

try this

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for pkg in ansible\_facts.packages|dict2items %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ pkg.value[0].name }} {{ pkg.value[0].version }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 29, 2021, 9:49am UTC](https://forum.ansible.com/t/package-facts-module/35798/3 "2021-09-29T09:49:10Z")

</div>

More versions of the same package might be installed. This is  
the reason why the items of the dictionary \*packages\* are lists. The  
code above will list only the first package from the lists. Iterate  
the lists if you want to be sure you get all packages, e.g.

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for l in ansible\_facts.packages|dict2items %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for pkg in l.value %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ pkg.name }} {{ pkg.version }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

This can be simplified

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for l in packages %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for p in packages[l] %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ p.name }} {{ p.version }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

---

<div class="post-metadata">

**Author:** ![John\_Veliss](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@John\_Veliss](https://forum.ansible.com/u/John_Veliss)\
**Post date:** [September 29, 2021, 11:38am UTC](https://forum.ansible.com/t/package-facts-module/35798/4 "2021-09-29T11:38:58Z")

</div>

Thanks Vladimir and Dick.

You guys are too clever.

If I want to learn how to do this, what should I be learning, python variables?

Can someone please explain what these lines actually mean?

Also, how can I get the output to display a package on seperate lines as mu output is like below.

“msg”: "kbd-misc 1.15.5\npth 2.0.7\niputils 20160308\nrhn-check 2.0.2\nperl-parent 0.225\nnumactl-libs 2.0.12\nrsyslog 8.24.0\ngdbm 1.10\ngpgme 1.3.2\ngrub2 2.02\nkatello-host-tools 3.5.4\nbzip2-libs 1.0.6\npython-setuptools 0.9.8\nunzip 6.0\nlibdb-utils 5.3.21\niwl2030-firmware 18.168.6.1\nlibcom\_err 1.42.9\nalsa-firmware 1.0.28\nselinux-policy 3.13.1\nbasesystem 10.0\nperl-Text-ParseWords 3.29\nredhat-support-tool 0.12.2\ndevice-mapper-persistent-data 0.8.5\ncryptsetup-libs 2.0.3\nlibselinux-utils 2.5\nperl-Scalar-List-Utils 1.27\nparted 3.1\npygobject2 2.28.6\nmozjs17 17.0.0\npython-firewall 0.6.3\npython-ipaddr 2.1.11\nlibxslt 1.1.28\nplymouth 0.8.9\nlibunistring 0.9.3\nkeyutils-libs 1.5.8\nperl-Encode 2.51\nlibgpg-error 1.12\nlvm2-libs 2.02.187\ngawk 4.0.2\naic94xx-firmware 30\nxmlsec1-openssl 1.2.20\njansson 2.10\ne2fsprogs-libs 1.42.9\npython-backports-ssl\_match\_hostname 3.5.0.1\nsubscription-manager-rhsm 1.24.48\niprutils 2.4.17.1\nfile-libs 5.11\npython-libs 2.7.5\ngrub2-tools 2.02\nlibxcb 1.13\npython-lxml 3.2.1\nlibnetfilter\_conntrack 1.0.6\nperl 5.16.3\ntzdata 2021a\nyum-metadata-parser 1.1.4\niwl3160-firmware 25.30.13.0\npython-gudev 147.2\nqrencode-libs 3.4.1\ndbus-python 1.1.1\npython-dateutil 1.5\nsetup 2.8.71\nkernel-tools-libs 3.10.0\nlibtasn1 4.10\nyum-rhn-plugin 2.0.1\nethtool 4.8\nsqlite 3.7.17\ntar 1.26\nsudo 1.8.23\nrhnsd 5.0.13\nfreetype 2.8\nlogrotate 3.8.6\nbind-license 9.11.4\nacl 2.2.51\npython-decorator 3.4.0\nalsa-lib 1.1.8\nlibdnet 1.12\ndejavu-sans-fonts 2.33\nebtables 2.0.10\nperl-constant 1.27\nyum-utils 1.1.31\npyliblzma 0.5.3\nncurses-libs 5.9\npolkit 0.112\nperl-Pod-Usage 1.63\npostfix 2.10.1\npciutils 3.5.1\npython-kitchen 1.1.1\ngpg-pubkey 2fa658e0\ngpg-pubkey fd431d51\nopenssh-clients 7.4p1\nlibuser 0.60\nperl-libs 5.16.3\nlibX11 1.6.7\nlz4 1.8.3\nkbd 1.15.5\nkexec-tools 2.0.15\npython-hwdata 1.7.3\nperl-Time-HiRes 1.9725\ngeoipupdate 2.5.0\nsystemd 219\nperl-File-Temp 0.23.01\ncoreutils 8.22\nlvm2 2.02.187\npython-slip-dbus 0.4.0\nlibacl 2.2.51\ndracut-config-rescue 033\nNetworkManager-config-server 1.18.8\nlibnl3 3.2.28\npolicycoreutils 2.5\nredhat-release-server 7.9\nredhat-logos 70.7.0

---

<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:** [September 29, 2021, 11:57am UTC](https://forum.ansible.com/t/package-facts-module/35798/5 "2021-09-29T11:57:22Z")

</div>

hii

that's just the way the output is formatted, which is json by default iirc.  
Using yaml can be cleaner.  
Add this to the [defaults] section of your .ansible.cfg :

stdout\_callback = yaml

Or, you could use the template/file module to write the file for you,  
so you don' need to copy/paste.

Dick

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 29, 2021, 6:30pm UTC](https://forum.ansible.com/t/package-facts-module/35798/6 "2021-09-29T18:30:10Z")

</div>

Start with Ansible introduction to YAML  
[https://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html)

and Jinja  
[https://jinja.palletsprojects.com/en/latest/](https://jinja.palletsprojects.com/en/latest/)

YAML specification explains the details  
[https://yaml.org/spec/1.2/spec.html](https://yaml.org/spec/1.2/spec.html)

---

<div class="post-metadata">

**Author:** ![John\_Veliss](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@John\_Veliss](https://forum.ansible.com/u/John_Veliss)\
**Post date:** [September 29, 2021, 10:35pm UTC](https://forum.ansible.com/t/package-facts-module/35798/7 "2021-09-29T22:35:30Z")

</div>

Thanks Vladimir, I’ll check them out.
