【问题标题】:Ansible error: "The Python 2 bindings for rpm are needed for this module"Ansible 错误:“此模块需要用于 rpm 的 Python 2 绑定”
【发布时间】:2020-05-30 10:07:21
【问题描述】:

我正在尝试使用以下任务在我的 python3 环境中安装需求文件

pip:
  python3: yes
  requirements: ./requirements/my_requirements.txt
  extra_args: -i http://mypypi/windows/simple

我检查了控制器节点 (RH7) 上运行的是哪个版本的 ansible,它是 3.6.8

ansible-playbook 2.9.9
  config file = None
  configured module search path = ['/home/{hidden}/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.8 (default, Jun 11 2019, 15:15:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
No config file found; using defaults

但是我收到以下错误:

fatal: [default]: FAILED! => {"changed": false, "msg": "The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module
instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead."}

我的控制器节点正在运行 RH7。 目标是 centos7(由 vagrantfiles 提供)

现在有人知道如何解决这个问题吗?

【问题讨论】:

  • 这不是你的控制器给你一个问题,而是你的节点,我会说。在节点上安装 python 3 将是一个解决方案。
  • 嗯,这就是我试图用 ansible 做的事情......这是我的 ansible 脚本中完成的第一件事,安装 python3。成功了。但是现在到了 pip 安装其他库的时候了..我得到了上述错误
  • 您是否按照错误消息的建议尝试使用dnf 模块?如果有,结果如何?
  • no..因为我不确定如果我要尝试安装在不同风格的操作系统上是否会失败。我所做的是使用 virtual_command 属性来显式调用 python3。 (不幸的是,我认为其中一些模块需要工作......似乎我也不得不依赖命令模块)

标签: python pip ansible


【解决方案1】:

我在使用 yum 的“Amazon Linux 2”发行版中遇到了类似的问题,但在撰写本文时不支持 dnf。

如上面的 cmets 所述,我的问题出在 ansible 托管节点(运行 Amazon Linux 2 的 AWS EC2 实例)中,而不是在控制器中。

通过强制使用python2解决,在ansible清单文件中为这组主机添加ansible_python_interpreter=/usr/bin/python2,如下sn-p:

[amz_linux]
server2 ansible_host=ec2-xx-yy-zz-pp.eu-west-1.compute.amazonaws.com


[amz_linux:vars]
ansible_user=ec2-user
ansible_python_interpreter=/usr/bin/python2

用这个剧本尝试过,改编自Redhat quick guide

---
- hosts: amz_linux
  become: yes
  tasks:
   - name: install Apache server
     yum:
       name: httpd
       state: latest

   - name: enable and start Apache server
     service:
       name: httpd
       enabled: yes
       state: started

   - name: create web admin group
     group:
       name: web
       state: present

   - name: create web admin user
     user:
       name: webadm
       comment: "Web Admin"
       groups: web
       append: yes

   - name: set content directory group/permissions 
     file:
       path: /var/www/html
       owner: root
       group: web
       state: directory
       mode: u=rwx,g=rwx,o=rx,g+s

   - name: create default page content
     copy:
       content: "Welcome to {{ ansible_fqdn}} on {{ ansible_default_ipv4.address }}"
       dest: /var/www/html/index.html
       owner: webadm
       group: web
       mode: u=rw,g=rw,o=r

实际 ansible-playbook 运行(使用 ssh-add 将实例私钥添加到 ssh 代理后。)

$ ansible-playbook -i ansible/hosts ansible/apache_amz_linux.yaml 

PLAY [amz_linux] **********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
The authenticity of host 'ec2-xxxxxxxxxxx.eu-west-1.compute.amazonaws.com (xxxxxxxxxxx)' can't be established.
ECDSA key fingerprint is SHA256:klksjdflskdjflskdfjsldkfjslkdjflskdjf/sdkfj.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
ok: [server2]

TASK [install Apache server] **********************************************************************************************
changed: [server2]

TASK [enable and start Apache server] *************************************************************************************
changed: [server2]

TASK [create web admin group] *********************************************************************************************
changed: [server2]

TASK [create web admin user] **********************************************************************************************
changed: [server2]

TASK [set content directory group/permissions] ****************************************************************************
changed: [server2]

TASK [create default page content] ****************************************************************************************
changed: [server2]

PLAY RECAP ****************************************************************************************************************
server2                    : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

【讨论】:

    【解决方案2】:

    如果您的ansible.cfg 文件的inventory 条目设置为不存在的文件,则此错误消息会显示。

    【讨论】:

      【解决方案3】:

      修改

      ansible-playbook
      

      ansible-playbook -e ansible_python_interpreter=/usr/bin/python2
      

      【讨论】:

        猜你喜欢
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多