Ansible 指定受控端使用Python的版本

最近在装Ansible,有一台受控端Ubuntu16的服务器,安装了Python2.7.12和Pyhon3.5。当用Ansible连接它时,显示使用的是Python3.5。最后看文档,发现Ansible可以在hosts的文件中指定受控服上运行的Python。

现象

受控端

  • 查看Python版本
bash 复制代码
python2 --version
Python 2.7.12
bash 复制代码
python3 --version
Python 3.5.2

主控端

  • 主控端连接被控端
bash 复制代码
ansible 192.168.8.101 -m shell -a 'ip a| grep 192.168.'
bash 复制代码
192.168.8.101 | FAILED | rc=-1 >>
ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.6. Current version: 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609]

解决

  • 修改hosts文件
bash 复制代码
vim /etc/ansible/hosts
bash 复制代码
192.168.8.101 ansible_python_interpreter=/usr/bin/python2.7
  • 执行效果
bash 复制代码
ansible 192.168.8.101 -m shell -a 'ip a| grep 192.168.'
bash 复制代码
192.168.8.101 | CHANGED | rc=0 >>
    inet 192.168.8.101/24 brd 192.168.8.255 scope global eno1

hosts配置参数

节选自: 如何指定ansible使用python3 ansible 指定host

相关推荐
m0_7349497917 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_5145205717 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey18 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
瑞瑞小同学18 小时前
git 常用相关操作
git
金銀銅鐵18 小时前
[git] 如何合并若干个 commit?(上)
git
Polar__Star19 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
m0_7436239219 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
FreakStudio19 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
gCode Teacher 格码致知20 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
不要秃头的小孩20 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划