pyenv装的2.7.14 无法用ansible yum.(其实不是pyenv的原因,自己编译安装的2.7.14也不行)
| 1 | #ansible OA* -m yum -a"name=mutt state=present" | 
使用系统默认的python作为解释器可以使用yum模块1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27[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不行1
2
3
4
5
6
7
8
9
10
11
12
13
14#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回来1
2
3
4
5
6
7
8
9
10
11
12
13
14
15- 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
