Ansible python2 yum module is needed for this module.md

pyenv装的2.7.14 无法用ansible yum.(其实不是pyenv的原因,自己编译安装的2.7.14也不行)

#ansible OA* -m yum -a"name=mutt state=present" 
OA_P | FAILED! => {
    "changed": false, 
    "msg": "python2 yum module is needed for this  module"
}
OA_S | FAILED! => {
    "changed": false, 
    "msg": "python2 yum module is needed for this  module"
}

#ansible OA* -m shell -a "which python pip"
OA_P | SUCCESS | rc=0 >>
/root/.pyenv/versions/2.7.14/bin/python
/root/.pyenv/versions/2.7.14/bin/pip
OA_S | SUCCESS | rc=0 >>
/root/.pyenv/versions/2.7.14/bin/python
/root/.pyenv/versions/2.7.14/bin/pip

#vi /etc/ansible/hosts/oa 
[OA]
OA_P ansible_connection=local
OA_S ansible_ssh_host=172.16.200.209 
[OA:vars]
ansible_python_interpreter=/root/.pyenv/shims/python

使用系统默认的python作为解释器可以使用yum模块

[root@OA_P 13:04:15 /etc/ansible/roles]
#ansible OA* -m yum -a"name=mutt state=present" 
OA_P | FAILED! => {
    "changed": false, 
    "msg": "python2 bindings for rpm are needed for this module. python2 yum module is needed for this  module"
}
OA_S | FAILED! => {
    "changed": false, 
    "msg": "python2 bindings for rpm are needed for this module. python2 yum module is needed for this  module"
}
^C
[root@OA_P 13:04:28 /etc/ansible/roles]
#ansible OA* -m yum -a"name=mutt state=present" -e "ansible_python_interpreter=/usr/bin/python"
OA_S | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "5:mutt-1.5.20-8.20091214hg736b6a.el6.x86_64 providing mutt is already installed"
    ]
}
OA_P | FAILED! => {
    "changed": false, 
    "msg": "The following packages have pending transactions: mutt-x86_64", 
    "rc": 125, 
    "results": []
}

这是因为2.6可以import yum而2.7不行

#python
Python 2.7.14 (default, Dec 15 2017, 23:08:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yum

#python2.6
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum

目前我的解决方法是, 在yum之前先set facts, 之后在set回来

- set_fact:
    ansible_python_default_interpreter: "{{ ansible_python_interpreter }}"
    ansible_python_interpreter:  "/usr/bin/python"

- name: 安装依赖包
  yum:
    name: "{{ item.line }}"
    state: present
  with_items:
    - { line: 'openssl' }
    - { line: 'openssl-devel' }
    - { line: 'mutt' }

- set_fact:
    ansible_python_interpreter: "{{ ansible_python_default_interpreter }}"

参考

https://github.com/openshift/openshift-ansible/issues/855
https://stackoverflow.com/questions/29711514/no-module-named-yum-error-with-python2-7-when-using-ansible-on-fedora/36138921#36138921

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值