3

I would like to check if my package is installed, but I would like to have all package types with a name.

Let me explain: I am looking for the OB2 package, but there are plenty of them, so I am looking for OB2* but it skips my search.

I tested with the name without wildcard (*) but it doesn't work any better:

- name: Gather the package facts
  ansible.builtin.package_facts:
    manager: auto

- name: Print the package facts
  ansible.builtin.debug:
    var: ansible_facts.packages
    
- name: Check whether a package called OB2 is installed
  ansible.builtin.debug:
    msg: "{{ ansible_facts.packages['OB2*'] | length }} versions of OB2are installed!"
  when: "'OB2*' in ansible_facts.packages"

Is there a solution to find all outputs that start with OB2 within ansible_facts.package?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83

2 Answers2

3

ansible_facts.packages is a dictionary. There are no keys in that dictionary named OB2* nor OB2 since no package has this exact name.

If you want to get all keys which names start with the string "OB2", one way is to filter out all others.

Here is a playbook illustrating the concept. For the example, I used as a prefix "zlib". Just change it back to whatever suits your needs

 ---
 - hosts: localhost
   gather_facts: false
 
   vars:
     package_prefix: "zlib"
 
     filtered_packages: "{{ ansible_facts.packages | dict2items 
       | selectattr('key', 'match', package_prefix) 
       | map(attribute='value') | flatten }}"
 
   tasks:
     - name: gather package facts
       ansible.builtin.package_facts:
 
     - name: debug the raw variable
       debug:
         var: filtered_packages
 
     - name: count relevant packages
       vars:
         pkg_num: "{{ filtered_packages | count }}"
       debug:
         msg: "There are {{ pkg_num }} packages
           which name starts with {{ package_prefix }}"
 
     - name: show some info about relevant packages
       debug:
         msg: "Package named {{ item.name }} is in category {{ item.category }}
           and has version {{ item.version }}"
       loop: "{{ filtered_packages }}"

Which gives on my Ubuntu local machine:

PLAY [localhost] *****************************************************************************************************************

TASK [gather package facts] ******************************************************************************************************
ok: [localhost]

TASK [debug the raw variable] ****************************************************************************************************
ok: [localhost] => {
    "filtered_packages": [
        {
            "arch": "amd64",
            "category": "libs",
            "name": "zlib1g",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "1:1.2.11.dfsg-2ubuntu1.3"
        },
        {
            "arch": "amd64",
            "category": "libdevel",
            "name": "zlib1g-dev",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "1:1.2.11.dfsg-2ubuntu1.3"
        }
    ]
}

TASK [count relevant packages] ***************************************************************************************************
ok: [localhost] => {
    "msg": "There are 2 packages which name starts with zlib"
}

TASK [show some info about relevant packages] ************************************************************************************
ok: [localhost] => (item={'name': 'zlib1g', 'version': '1:1.2.11.dfsg-2ubuntu1.3', 'arch': 'amd64', 'category': 'libs', 'origin': 'Ubuntu', 'source': 'apt'}) => {
    "msg": "Package named zlib1g is in category libs and has version 1:1.2.11.dfsg-2ubuntu1.3"
}
ok: [localhost] => (item={'name': 'zlib1g-dev', 'version': '1:1.2.11.dfsg-2ubuntu1.3', 'arch': 'amd64', 'category': 'libdevel', 'origin': 'Ubuntu', 'source': 'apt'}) => {
    "msg": "Package named zlib1g-dev is in category libdevel and has version 1:1.2.11.dfsg-2ubuntu1.3"
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
3

You have to filter the keys, a direct address is not possible, because you don't know the exact name.

- name: Gather the package facts
  ansible.builtin.package_facts:

- name: Filter package names
  set_fact:
    filtered_package_names: "{{ ansible_facts.packages | list 
      | map('regex_search', '^vim.*') | select('string') | list }}"
  
- name: Print filtered packages
  debug:
    var: filtered_package_names

- name: Print package details from all filtered packages
  debug:
    msg: "{{ ansible_facts.packages[item] }}"
  with_items: "{{ filtered_package_names }}"

With list a list of the keys is created, then you can filter this list with regex_search, afterwards the list is reduced to the filter result.

== Edit begin

There is a smarter filter method. Instead of using map(regex_search) / select(string), you could use directly select(match), so the filtering would look like:

- name: Filter package names
  set_fact:
    filtered_package_names: "{{ ansible_facts.packages | list 
      | select('match', '^vim.*') | list }}"

== Edit end

The result is a list of package names that match your regex.

If you need more information about one of the packages, you can then use ansible_facts.packages[_your_item] to get the rest of the information.

Example output of the above tasks:

TASK [Gather the package facts] ****************************************************************************************
ok: [localhost]

TASK [Filter package names] ********************************************************************************************
ok: [localhost]

TASK [Print filtered packages] *****************************************************************************************
ok: [localhost] => {
    "filtered_package_names": [
        "vim",
        "vim-common",
        "vim-runtime",
        "vim-tiny"
    ]
}

TASK [Print package details] *******************************************************************************************
ok: [localhost] => (item=vim) => {
    "msg": [
        {
            "arch": "amd64",
            "category": "editors",
            "name": "vim",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "2:8.1.2269-1ubuntu5.7"
        }
    ]
}
ok: [localhost] => (item=vim-common) => {
    "msg": [
        {
            "arch": "all",
            "category": "editors",
            "name": "vim-common",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "2:8.1.2269-1ubuntu5.7"
        }
    ]
}
ok: [localhost] => (item=vim-runtime) => {
    "msg": [
        {
            "arch": "all",
            "category": "editors",
            "name": "vim-runtime",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "2:8.1.2269-1ubuntu5.7"
        }
    ]
}
ok: [localhost] => (item=vim-tiny) => {
    "msg": [
        {
            "arch": "amd64",
            "category": "editors",
            "name": "vim-tiny",
            "origin": "Ubuntu",
            "source": "apt",
            "version": "2:8.1.2269-1ubuntu5.7"
        }
    ]
}
phanaz
  • 1,188
  • 8
  • 17