package_facts module

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

try this

    - debug:
        msg: |
          {% for pkg in ansible_facts.packages|dict2items %}
          {{ pkg.value[0].name }} {{ pkg.value[0].version }}
          {% endfor %}

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.

    - debug:
        msg: |
          {% for l in ansible_facts.packages|dict2items %}
          {% for pkg in l.value %}
          {{ pkg.name }} {{ pkg.version }}
          {% endfor %}
          {% endfor %}

This can be simplified

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

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

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

Start with Ansible introduction to YAML
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

and Jinja
https://jinja.palletsprojects.com/en/latest/

YAML specification explains the details
https://yaml.org/spec/1.2/spec.html

Thanks Vladimir, I’ll check them out.