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.

New contributor

user26423314 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

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

  • Checking Python version through Ansible

and others which might be helpful in this case

  • Ansible: Install Python module only if it not exist
  • Ansible: How to install a package only if same version not installed?

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật