spiros_m
spiros_m

Reputation: 41

Ansible: Select versions of packages from the output of package_facts

I'm running a Playbook which collects the packages facts from the remote hosts, what I want to achieve is to filter the output and grab only the name and the version of the packages.

This is whats in my playbook

- name: Gather rpm packages
  package_facts:
    manager: auto

- name: Print the rpm packages
  debug:
    var: ansible_facts.packages

And the result format

TASK [infra_pt : Print the rpm packages] ****************************************************
ok: [192.168.47.135] => {
    "msg": {
        "GConf2": [
            {
                "arch": "x86_64", 
                "epoch": null, 
                "name": "GConf2", 
                "release": "8.el7", 
                "source": "rpm", 
                "version": "3.2.6"
            }
        ], 
        "GeoIP": [
            {
                "arch": "x86_64", 
                "epoch": null, 
                "name": "GeoIP", 
                "release": "9.el7", 
                "source": "rpm", 
                "version": "1.5.0"
            }
        ], 

The preferable output will only include name and version number, how can be archived ?

Upvotes: 0

Views: 3812

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68004

There might be more versions of the same package installed. Let's take the first one.

      - debug:
          msg: "{{ item.key }} {{ item.value.0.version }}"
        loop: "{{ ansible_facts.packages|dict2items }}"

gives

    msg: GeoIP 1.5.0
    msg: GConf2 3.2.6

Use json_query if you want to get the lists of the installed versions

    - debug:
        msg: "{{ item.key }} {{ item.value|json_query('[].version') }}"
      loop: "{{ ansible_facts.packages|dict2items }}"

gives

    msg: GeoIP ['1.5.0']
    msg: GConf2 ['3.2.6']

Upvotes: 1

Related Questions