获取远程可用的Python版本

问题描述 投票:0回答:1

我有一些具有不同属性和不同安装的 Python 版本的机器。现在我想编写一个任务,返回每台机器上所有可用的 Python 版本(有些是 2.7.x,有些是 3.8.x,还有一些介于两者之间)。

根据情况,我想注册最高版本以用于后续任务。

我尝试过滤

ansible_facts.packages
的输出,但在 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!" 

我需要它来返回机器以及该特定机器上的每个 python 版本。

python ansible
1个回答
0
投票

在使用 Python 3.9.18 的 RHEL 9.4 系统上,一个最小的示例手册

---
- 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*'

将导致

ansible_python_version
简单地变为

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

来自 Ansible 解释器发现

对于

ansible_facts.packages
它将导致输出

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

如果选择了以

python
开头的所有键。人们可以看一下

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

类似的问答和文档

© www.soinside.com 2019 - 2024. All rights reserved.