You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible收集Facts无法获取ansible_distribution_release及setup模块报错问题排查

解决Ansible 2.5.1在Ubuntu 20.04上的setup模块警告与变量未定义问题

问题重现

当你在Ubuntu 20.04桌面版主机上执行命令:

ansible -m setup <hostname>

会收到如下警告:

[WARNING]: Module invocation had junk after the JSON data: AttributeError("module 'platform' has no attribute 'dist'")

同时输出结果里没有ansible_distribution_release字段,进而导致依赖该变量的Ansible任务执行失败,错误提示:

'ansible_distribution_release' is undefined

问题根源

这个问题出在你使用的Ansible 2.5.1版本上——该版本的setup模块依赖Python的platform.dist()方法来获取发行版信息,但Ubuntu 20.04默认搭载的Python 3.8及以后版本,已经移除了platform.dist()这个被废弃的方法,直接导致模块执行出错,无法正常收集ansible_distribution_release这类发行版相关变量。

解决方案

你已经通过升级Ansible至新版本解决了这个问题,这也是最直接有效的方案:

  • Ansible在后续版本(比如2.9及以上)中已经修复了这个兼容性问题,替换了过时的platform.dist()调用,改用更稳定的方式获取系统发行版信息,能够在Ubuntu 20.04这类搭载Python 3.8+的系统上正常运行setup模块,完整收集所需的变量。

如果暂时无法升级,也可以尝试在目标主机上手动安装distro库(pip install distro),部分旧版本Ansible会 fallback 到这个库来获取发行版信息,但这只是临时 workaround,长远来看升级Ansible版本是更可靠的选择。

内容的提问来源于stack exchange,提问作者Vince

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:02:32