当前位置: 代码迷 >> 综合 >> ansible 配置python interpreter解释环境的几种方法
  详细解决方案

ansible 配置python interpreter解释环境的几种方法

热度:5   发布时间:2023-12-15 22:57:01.0

因为centos默认安装Python2,但是现在python2已经离我们而去,很多ansible module都更好的依赖于python3 ,所以我们经常会遇到ansible版本和python版本不兼容的问题
可能你遇到的报错有如下字眼,就说明遇到了缺少必要python库或者ansible 模块跟python版本不匹配问题

An exception occurred during task execution. To see the full
traceback, use -vvv. The error was: ImportError: No module named
kubernetes fatal: [k8s-master1]: FAILED! => {“changed”: false,
“error”: “No module named kubernetes”, “msg”: “Failed to import the
required Python library (openshift) on kubernetesM01’s Python
/usr/bin/python. Please read module documentation and install in the
appropriate location. If the required library is installed, but
Ansible is using the wrong Python interpreter, please consult the
documentation on ansible_python_interpreter”}

你可以选择在当前Python环境安装python依赖包解决问题,注意:这里的安装包是需要安装在被控制服务器(目标服务器)上的 ; 当然如果系统安装了python3也可以选择python3解释器

  1. 运行命令时添加
ansible localhost -m ping -e 'ansible_python_interpreter=/usr/bin/python3'
ansible-playbook sample-playbook.yml -e 'ansible_python_interpreter=/usr/bin/python3'
  1. 添加在ansible.cfg 中
localhost-py3 ansible_host=localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python3
k8s-master1 ansible_host=192.100.30.164 ansible_ssh_pass=youpassword
  1. 添加在ansible-playbook剧本中
    在tasks中声明
- hosts: test_servertasks:- name: install pipapt:name: python-pipvars:ansible_python_interpreter: /usr/bin/python

在剧本的范围内

- hosts: test_servervars:ansible_python_interpreter: /usr/bin/pythontasks:- name: install pipapt:name: python-pip