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

如何解决Ansible Molecule执行测试时的‘python not found’错误?

解决Molecule测试中Python解释器找不到的问题

问题本质

报错/bin/sh: python: command not found的核心原因是:目标Docker镜像中不存在python可执行文件。当前多数现代Linux发行版(如Ubuntu 22.04、Debian 11+)仅默认安装python3,未创建python到python3的软链接,但Ansible默认会尝试调用python命令。


解决方案

方案1:在Molecule场景配置中指定Python解释器

编辑角色目录下的molecule/default/molecule.yml,添加Python解释器配置:

provisioner:
  name: ansible
  config_options:
    defaults:
      ansible_python_interpreter: /usr/bin/python3

或者直接在平台定义中为目标实例指定:

platforms:
  - name: instance
    image: ubuntu:22.04
    host_vars:
      ansible_python_interpreter: /usr/bin/python3

方案2:使用兼容镜像或预安装软链接包

  • 选择自带python软链接的镜像,比如ubuntu:20.04;
  • 或通过自定义Dockerfile在镜像中安装依赖:
    1. 修改molecule/default/molecule.yml:
    platforms:
      - name: instance
        image: ubuntu:22.04
        pre_build_image: true
        dockerfile: Dockerfile.j2
    
    1. 创建molecule/default/Dockerfile.j2:
    FROM {{ item.image }}
    RUN apt-get update && apt-get install -y python-is-python3 sudo
    
    该步骤会自动在镜像中创建python到python3的软链接。

方案3:确保Molecule读取正确的ansible.cfg

Molecule按以下优先级加载配置文件:

  1. 当前工作目录下的ansible.cfg
  2. 用户主目录的~/.ansible.cfg
  3. 系统级的/etc/ansible/ansible.cfg

如果你的ansible.cfg不在上述路径,可通过环境变量指定:

export ANSIBLE_CONFIG=/path/to/your/ansible.cfg

验证修复

修改完成后重新执行测试:

molecule test

若仍有问题,通过debug模式确认配置是否生效:

molecule --debug test

检查输出中ANSIBLE_PYTHON_INTERPRETER的值是否为/usr/bin/python3。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:15:42