我正在编写 Ansible 剧本来构建一个使用 Raspi OS 在 4 个节点上运行的 Kubernetes 集群 (K3S)。
我正在使用 kubernetes.core 包,可以使用以下命令安装在 Ansible 中:
ansible-galaxy collection install kubernetes.core
API 可以在这里找到。
我正在尝试使用类似于以下命令的任务将
cert-manager
存储库安装到 Helm
:
helm repo add jetstack https://charts.jetstack.io --force-update
到目前为止,我将任务定义为:
- name: Add cert-manager Helm repository
kubernetes.core.helm_repository:
name: jetstack
url: https://charts.jetstack.io
force: true
但是当我运行我的剧本时,出现以下错误:
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'
有谁知道怎么回事吗?
您需要安装
pyyaml
python 模块。如果您认为它已经安装,那么您的 ansible 机器上可能有多个 Python 版本。
您可以通过以下方式找到 ansible 使用的 Python 版本:
$ ansible --version
ansible 2.10.17
...
configured module search path = ['/Users/v_angapov/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.18 (main, Aug 24 2023, 18:16:58) [Clang 15.0.0 (clang-1500.0.40.1)]
在我的例子中,Ansible 使用的是 Python 3.9.18。
然后你可以尝试手动导入yaml模块:
$ python3.9 --version
Python 3.9.18
$ python3.9 -c "import yaml"
$
我强烈建议对 ansible 使用
python-virtualenv
:https://www.redhat.com/sysadmin/python-venv-ansible