Ansible Molecule忽略Git类型Collection依赖问题排查
问题:Molecule无法安装Git类型Ansible Collection,导致模块/过滤器无法找到
环境版本
molecule 4.0.4 using python 3.9 ansible:2.14.1 delegated:4.0.4 from molecule docker:2.1.0 from molecule_docker requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0
当前配置
molecule.yml依赖部分
--- dependency: name: galaxy options: requirements-file: collections.yml ...
collections.yml
--- collections: - name: https://github.com/acoby/ansible-collection.git type: git version: main
错误日志
$ molecule test ... INFO Running default > dependency WARNING Skipping, missing the requirements file. WARNING Skipping, missing the requirements file. ... An exception occurred during task execution. To see the full traceback, use -vvv. The error was: #eof. Could not load "acoby.collection.my_urlencode": 'Invalid plugin FQCN (acoby.collection.my_urlencode): unable to locate collection acoby.collection' failed: [instance] (item={'src': 'mail.rc.j2', 'dest': '/root/.mailrc', 'mode': '0440'}) ...
问题原因及解决方法
1. 配置文件路径未正确识别
Molecule提示Skipping, missing the requirements file.,说明未找到指定的collections.yml:
- 确保文件放在Molecule场景目录(如
roles/your_role/molecule/default/),若放在角色根目录,需在molecule.yml中指定相对路径:dependency: name: galaxy options: requirements-file: ../../collections.yml - 可手动验证路径:在场景目录执行
ls ../../collections.yml,确认文件存在。
2. Collections.yml格式不符合Ansible Galaxy规范
Git类型的collection配置需区分name(collection的FQCN,即acoby.collection)和source(Git仓库地址),原配置将Git地址写在name字段,导致无法识别:
--- collections: - name: acoby.collection source: https://github.com/acoby/ansible-collection.git type: git version: main
- 先手动执行
ansible-galaxy collection install -r collections.yml,验证collection能正常安装,排除Ansible本身的问题。
3. Molecule与Ansible版本兼容性优化
Molecule 4.x与Ansible 2.14.x需确保依赖组件兼容:
- 执行
pip install --upgrade ansible molecule molecule-docker,更新到最新兼容版本。 - 若手动安装成功但Molecule仍失败,可添加
force: true参数强制重新安装:dependency: name: galaxy options: requirements-file: collections.yml force: true
4. 验证临时目录的Collection安装情况
Molecule会将collection安装在临时缓存目录(如~/.cache/molecule/your_role/default/collections),运行molecule test后可检查该目录下是否存在acoby.collection,确认安装状态。
内容的提问来源于stack exchange,提问作者TRW
相关产品推荐
相关产品推荐

