Search code examples
pythonansible

How to get the available Python versions on Remote Node?


I have some machines with different properties and different installed versions of Python. Now I want to write a task that returns all available Python versions on every machine (some have 2.7.x, some 3.8.x and others are in between).

Depending on that I want to register the highest version for later tasks.

I tried to filter the output of ansible_facts.packages but I fail at using filters in YAML.

- name: Gather the package facts 
  ansible.builtin.package_facts: 
    manager: auto 
    strategy: all 
- name: Check whether a package called python is installed 
  ansible.builtin.debug: 
    msg: "{{ ansible_facts.packages['python*'] }} versions of foobar are installed!" 

I need it to return the machine and every Python version on that specific machine.


Solution

  • On a RHEL 9.4 System with Python 3.9.18 a minimal example playbook

    ---
    - hosts: localhost
      become: true
      gather_facts: true
    
      pre_tasks:
    
      - package_facts:
    
      tasks:
    
      - debug:
          var: ansible_python_version
    
      - debug:
          msg: "{{ ansible_facts.packages[item] }}"
        loop: "{{ ansible_facts.packages | select('search', regex) }}"
        vars:
          regex: 'python*'
    
    

    will result for ansible_python_version simply into an output of

    TASK [debug] *******************
    ok: [localhost] =>
      ansible_python_version: 3.9.18
    

    from Ansible Interpreter Discovery.

    For ansible_facts.packages it will result into an output of

    TASK [debug] ************************************************
    ok: [localhost] => (item=python3-setuptools-wheel) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-setuptools-wheel
        release: 12.el9
        source: rpm
        version: 53.0.0
    ok: [localhost] => (item=python3-setuptools) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-setuptools
        release: 12.el9
        source: rpm
        version: 53.0.0
    ok: [localhost] => (item=python3-dbus) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-dbus
        release: 2.el9
        source: rpm
        version: 1.2.18
    ok: [localhost] => (item=python3-six) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-six
        release: 9.el9
        source: rpm
        version: 1.15.0
    ok: [localhost] => (item=python3-gobject-base-noarch) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-gobject-base-noarch
        release: 6.el9
        source: rpm
        version: 3.40.1
    ok: [localhost] => (item=python3-gobject-base) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-gobject-base
        release: 6.el9
        source: rpm
        version: 3.40.1
    ok: [localhost] => (item=python3-iniparse) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-iniparse
        release: 45.el9
        source: rpm
        version: '0.4'
    ok: [localhost] => (item=python3-inotify) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-inotify
        release: 25.el9
        source: rpm
        version: 0.9.6
    ok: [localhost] => (item=python3-distro) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-distro
        release: 7.el9
        source: rpm
        version: 1.5.0
    ok: [localhost] => (item=python3-libcomps) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-libcomps
        release: 1.el9
        source: rpm
        version: 0.1.18
    ok: [localhost] => (item=python3-chardet) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-chardet
        release: 5.el9
        source: rpm
        version: 4.0.0
    ok: [localhost] => (item=python3-decorator) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-decorator
        release: 6.el9
        source: rpm
        version: 4.4.2
    ok: [localhost] => (item=python3-ethtool) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-ethtool
        release: 2.el9
        source: rpm
        version: '0.15'
    ok: [localhost] => (item=python3-pysocks) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pysocks
        release: 12.el9
        source: rpm
        version: 1.7.1
    ok: [localhost] => (item=python3-pyyaml) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-pyyaml
        release: 6.el9
        source: rpm
        version: 5.4.1
    ok: [localhost] => (item=python3-systemd) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-systemd
        release: 18.el9
        source: rpm
        version: '234'
    ok: [localhost] => (item=python3-gpg) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-gpg
        release: 6.el9
        source: rpm
        version: 1.15.1
    ok: [localhost] => (item=python3-ptyprocess) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-ptyprocess
        release: 12.el9
        source: rpm
        version: 0.6.0
    ok: [localhost] => (item=python3-pexpect) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pexpect
        release: 7.el9
        source: rpm
        version: 4.8.0
    ok: [localhost] => (item=python3-dateutil) =>
      msg:
      - arch: noarch
        epoch: 1
        name: python3-dateutil
        release: 7.el9
        source: rpm
        version: 2.8.1
    ok: [localhost] => (item=python3-pyparsing) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pyparsing
        release: 1.el9ap
        source: rpm
        version: 3.0.9
    ok: [localhost] => (item=python3-packaging) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-packaging
        release: 2.el9ap
        source: rpm
        version: '21.3'
    ok: [localhost] => (item=python3-pycparser) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pycparser
        release: 2.el9pc
        source: rpm
        version: '2.21'
    ok: [localhost] => (item=python3-cffi) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-cffi
        release: 3.el9ap
        source: rpm
        version: 1.15.0
    ok: [localhost] => (item=python3-cryptography) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-cryptography
        release: 1.el9ap
        source: rpm
        version: 38.0.4
    ok: [localhost] => (item=python3-resolvelib) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-resolvelib
        release: 5.el9
        source: rpm
        version: 0.5.4
    ok: [localhost] => (item=python3-xmltodict) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-xmltodict
        release: 15.el9
        source: rpm
        version: 0.12.0
    ok: [localhost] => (item=python3-pip-wheel) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pip-wheel
        release: 8.el9
        source: rpm
        version: 21.2.3
    ok: [localhost] => (item=python3-sssdconfig) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-sssdconfig
        release: 6.el9_4
        source: rpm
        version: 2.9.4
    ok: [localhost] => (item=python3-file-magic) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-file-magic
        release: 16.el9
        source: rpm
        version: '5.39'
    ok: [localhost] => (item=python3-libselinux) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-libselinux
        release: 1.el9
        source: rpm
        version: '3.6'
    ok: [localhost] => (item=python3-libsemanage) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-libsemanage
        release: 1.el9
        source: rpm
        version: '3.6'
    ok: [localhost] => (item=python3-setools) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-setools
        release: 1.el9
        source: rpm
        version: 4.4.4
    ok: [localhost] => (item=python3-urllib3) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-urllib3
        release: 5.el9
        source: rpm
        version: 1.26.5
    ok: [localhost] => (item=python3-requests) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-requests
        release: 8.el9
        source: rpm
        version: 2.25.1
    ok: [localhost] => (item=python3-cloud-what) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-cloud-what
        release: 1.el9
        source: rpm
        version: 1.29.40
    ok: [localhost] => (item=python3-audit) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-audit
        release: 2.el9
        source: rpm
        version: 3.1.2
    ok: [localhost] => (item=python3-sss) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-sss
        release: 6.el9_4
        source: rpm
        version: 2.9.4
    ok: [localhost] => (item=python3-librepo) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-librepo
        release: 2.el9
        source: rpm
        version: 1.14.5
    ok: [localhost] => (item=python3-libdnf) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-libdnf
        release: 8.el9
        source: rpm
        version: 0.69.0
    ok: [localhost] => (item=python3-hawkey) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-hawkey
        release: 8.el9
        source: rpm
        version: 0.69.0
    ok: [localhost] => (item=python3-policycoreutils) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-policycoreutils
        release: 2.1.el9
        source: rpm
        version: '3.6'
    ok: [localhost] => (item=python3-rpm) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-rpm
        release: 29.el9
        source: rpm
        version: 4.16.1.3
    ok: [localhost] => (item=python3-subscription-manager-rhsm) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-subscription-manager-rhsm
        release: 1.el9
        source: rpm
        version: 1.29.40
    ok: [localhost] => (item=python3-dnf) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-dnf
        release: 9.el9
        source: rpm
        version: 4.14.0
    ok: [localhost] => (item=python3-dnf-plugins-core) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-dnf-plugins-core
        release: 13.el9
        source: rpm
        version: 4.3.0
    ok: [localhost] => (item=python3-pip) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-pip
        release: 8.el9
        source: rpm
        version: 21.2.3
    ok: [localhost] => (item=python3-idna) =>
      msg:
      - arch: noarch
        epoch: null
        name: python3-idna
        release: 7.el9_4.1
        source: rpm
        version: '2.10'
    ok: [localhost] => (item=python3-libs) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3-libs
        release: 3.el9_4.1
        source: rpm
        version: 3.9.18
    ok: [localhost] => (item=python-unversioned-command) =>
      msg:
      - arch: noarch
        epoch: null
        name: python-unversioned-command
        release: 3.el9_4.1
        source: rpm
        version: 3.9.18
    ok: [localhost] => (item=python3) =>
      msg:
      - arch: x86_64
        epoch: null
        name: python3
        release: 3.el9_4.1
        source: rpm
        version: 3.9.18
    

    if all keys are selected which begins with python. One may also have a look into the output of

      - debug:
          msg: "{{ ansible_facts.packages.keys() }}"
    

    The issue with the initial question was ansible_facts.packages['python*'], as globbing in the variable name and the dict will not work. One need to select the dict keys differently and as shown above.

    Similar Q&A

    and others which might be helpful in this case