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

本文解决Ansible在CentOS上因默认Python2导致的与Python3不兼容问题,介绍如何配置Ansible使用Python3解释器,并提供具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为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_server
  tasks:
    - name: install pip
      apt:
        name: python-pip
      vars:
        ansible_python_interpreter: /usr/bin/python

在剧本的范围内

- hosts: test_server
  vars:
    ansible_python_interpreter: /usr/bin/python
  tasks:
    - name: install pip
      apt:
        name: python-pip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值