【问题标题】:Ansible and Amazon Linux 2: How can I use yum module with Python3?Ansible 和 Amazon Linux 2:如何在 Python3 中使用 yum 模块?
【发布时间】:2021-06-08 22:36:32
【问题描述】:

我使用 Ansible 2.9 通过 Amazon Linux 2 创建 EC2 实例。出于某些目的,我需要 EC2 上的 Python3。

所以我使用选项ansible_python_interpreter: "/usr/bin/python3"

但是使用这个选项模块 yum 返回错误pkg_mgr: yum msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

但 Amazon Lunux 2 不适用于 dnf

Ansible error: "The Python 2 bindings for rpm are needed for this module" 和其他论坛中描述了相同的问题。到处都建议的解决方案是 Python2。

有什么方法可以使用 Python3 和 yum 吗?还是唯一的方法是使用 shell 模块?

【问题讨论】:

  • yum 尚未移植到 python 3。您必须使用 python 2。

标签: python amazon-web-services ansible amazon-linux-2


【解决方案1】:

根据从other site收集的信息, 您可以将 yum 和非 yum 任务分开,只将 python3 用于非 yum 任务:

- hosts: testsv
  gather_facts: no
  become: yes
 
  tasks:
    # here yum running under python2 without errors
    - name: task1 
      yum:
        list: curl  

    # here yum running under python3
    - name: task2 
      yum:
        list: curl
      vars:
        ansible_python_interpreter: /usr/bin/python3

或者尝试反转条件:

- hosts: testsv
  gather_facts: no
  become: yes
 
  tasks:
    # Run yum under python2, and all other tasks under python3
    - name: task2 
      yum:
        list: curl
      vars:
        ansible_python_interpreter: /usr/bin/python2

【讨论】:

    【解决方案2】:

    @rzlvmp 的解决方案基本上可以,谢谢。 但是 yum 无论如何都使用默认 python,因为在 /bin/yum 解释器是 #!/usr/bin/python

    那么,有两个选择:

    1. 使用 vars: ansible_python_interpreter: /usr/bin/python3 执行特殊任务,默认使用 Python2。

    2. 编辑/bin/yum
      #!/usr/bin/python -> #!/usr/bin/python2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 2015-02-05
      • 1970-01-01
      • 2018-04-22
      • 2021-09-22
      • 1970-01-01
      相关资源
      最近更新 更多