package_facts module

170 views
Skip to first unread message

John Veliss

unread,
Sep 28, 2021, 9:56:22 PM9/28/21
to Ansible Project
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

Dick Visser

unread,
Sep 29, 2021, 4:55:32 AM9/29/21
to ansible...@googlegroups.com
try this

- debug:
msg: |
{% for pkg in ansible_facts.packages|dict2items %}
{{ pkg.value[0].name }} {{ pkg.value[0].version }}
{% endfor %}
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/14022329-fda1-4e9c-86c9-550412003f0en%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Vladimir Botka

unread,
Sep 29, 2021, 5:49:27 AM9/29/21
to Dick Visser, ansible...@googlegroups.com
On Wed, 29 Sep 2021 10:55:06 +0200
Dick Visser <dick....@geant.org> wrote:

> 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 %}

--
Vladimir Botka

John Veliss

unread,
Sep 29, 2021, 7:39:20 AM9/29/21
to ansible...@googlegroups.com
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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Sep 29, 2021, 7:57:48 AM9/29/21
to ansible...@googlegroups.com
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
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAP-ZCbmg%2B8UpdeFpqrDssaWj%3D26p%3Dr5riBhh0yd99Q2r3hyREw%40mail.gmail.com.

Vladimir Botka

unread,
Sep 29, 2021, 2:30:26 PM9/29/21
to John Veliss, ansible...@googlegroups.com
On Wed, 29 Sep 2021 21:38:58 +1000
John Veliss <vel...@gmail.com> wrote:

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

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

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


--
Vladimir Botka

John Veliss

unread,
Sep 29, 2021, 6:35:51 PM9/29/21
to Vladimir Botka, ansible...@googlegroups.com
Thanks Vladimir, I'll check them out. 
Reply all
Reply to author
Forward
0 new messages