DEBUG.WIKI
RackNerd;$21.99/年 · 1GB KVM
搬瓦工;先看线路,再选 VPS;CN2 GIA · 对应线路套餐;查看线路 →
腾讯云;让你的想法跑起来;轻量服务器 · 建站 / Docker / API;查看产品 →

解决 Ansible 警告 Platform using discovered Python 异常

解决 Ansible 运行在 macOS/Linux 节点时反复弹出 discovered Python interpreter 警告的 ansible_python_interpreter 显式配置方案。

问题现象

运行 Ansible 任务时,每个 Task 前面都打印黄色警告日志:

[WARNING]: Platform darwin on host node-mac is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.

触发根因

  1. Ansible 2.8+ 引入了自动探测 Python 解释器机制(Interpreter Discovery)。
  2. 当目标主机未显式指定 ansible_python_interpreter 时,Ansible 会在每次建连时自动探测并给出提示警告,在存在多个 Python 版本的受控机上可能导致行为不确定。

解决步骤与操作命令

步骤一:在 inventory 文件中显式固定 Python 解释器

在 hosts.ini 中为组或主机显式指定路径:

[all:vars]
ansible_python_interpreter = /usr/bin/python3

步骤二:在 ansible.cfg 全局设定解释器探测策略

编辑 ansible.cfg:

[defaults]
# 静默已知警告
interpreter_python = auto_silent

配置为 auto_silent 后,Ansible 会继续使用自动探测机制,但不再向控制台输出任何冗余黄色警告。

验证确认

再次运行 Playbook:

ansible-playbook -i hosts.ini site.yml

终端输出整洁清爽,警告完全被抑制。