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

通过Ansible为K3S集群安装cert-manager时遇ModuleNotFoundError求助

问题:Ansible helm_repository模块报错ModuleNotFoundError: No module named 'yaml'

我正在编写Ansible playbook,为4个运行Raspi OS的节点构建K3S Kubernetes集群,已通过以下命令安装kubernetes.core集合:

ansible-galaxy collection install kubernetes.core

尝试通过Ansible任务添加cert-manager的Helm仓库,对应原生Helm命令为:

helm repo add jetstack https://charts.jetstack.io --force-update

定义的Ansible任务如下:

- name: Add cert-manager Helm repository
  kubernetes.core.helm_repository:
    name: jetstack
    url: https://charts.jetstack.io
    force: true

运行playbook时触发错误:

TASK [helm : Add cert-manager Helm repository]
***************************************************
An exception occurred during task execution. To see
the full traceback, use -vvv. The error was: 
ModuleNotFoundError: No module named 'yaml'

添加-vvv参数后得到完整回溯:

Traceback (most recent call last):
  File "/tmp/ansible_kubernetes.core.helm_repository_payload_tdbnu9cy/ansible_kubernetes.core.helm_repository_payload.zip/ansible_collections/kubernetes/core/plugins/modules/helm_repository.py", line 173, in <module>
ModuleNotFoundError: No module named 'yaml'

原因与解决方案

原因

kubernetes.core集合的helm_repository模块依赖Python的PyYAML库处理YAML格式配置,而你的Raspi OS目标节点未安装该库,导致模块执行时触发ModuleNotFoundError。

解决步骤

在执行helm_repository任务前,先给目标节点安装PyYAML依赖:

方法1:通过系统包管理器安装(推荐Raspi OS)

- name: Install PyYAML dependency via apt
  apt:
    name: python3-yaml
    state: present
    update_cache: yes

方法2:通过pip安装

如果目标节点使用pip管理Python包,也可以用以下任务:

- name: Install PyYAML via pip
  pip:
    name: pyyaml
    state: present

确保上述依赖安装任务在Add cert-manager Helm repository任务之前执行,即可解决该错误。


内容的提问来源于stack exchange,提问作者Lasse Michael Mølgaard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:22:08