gpt4 book ai didi

kubernetes - 如何使用 Kubectl 命令通过 Ansible 访问 Rancher 集群

转载 作者:行者123 更新时间:2023-12-02 11:46:31 26 4
gpt4 key购买 nike

我目前正在开发一个项目,我需要使用 Ansible 获取在 Rancher 上运行的 Kubernetes 集群的 pod 名称。这里的主要问题是我有几个问题阻碍了我前进。
我目前正在执行一个剧本来尝试检索此信息,而不是运行 CLI 命令,因为我想稍后操作那些 Rancher 机器(例如安装一个 rpm 文件)。
这是我正在执行的剧本,试图从 Rancher 中检索 pod 的名称:

---

- hosts: localhost
connection: local
remote_user: root
roles:
- role: ansible.kubernetes-modules
- role: hello-world
vars:
ansible_python_interpreter: '{{ ansible_playbook_python }}'

collections:
- community.kubernetes

tasks:
-
name: Gather openShift Dependencies
python_requirements_facts:
dependencies:
- openshift

-
name: Get the pods in the specific namespace
k8s_info:
kubeconfig: '/etc/ansible/RCCloudConfig'
kind: Pod
namespace: redmine
register: pod_list

-
name: Print pod names
debug:
msg: "pod_list: {{ pod_list | json_query('resources[*].status.podIP') }} "

- set_fact:
pod_names: "{{pod_list|json_query('resources[*].metadata.name')}}"

问题是每次尝试运行 playbook 时都会出现 Kubernetes 模块错误:
ERROR! the role 'ansible.kubernetes-modules' was not found in community.kubernetes:ansible           .legacy:/etc/ansible/roles:/home/jcp/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/           roles:/etc/ansible

The error appears to be in '/etc/ansible/GetKubectlPods': line 7, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

roles:
- role: ansible.kubernetes-modules
^ here

如果我在代码中删除该行,在我尝试检索该角色的位置,我仍然会收到类似的错误:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'kubernetes'
fatal: [localhost]: FAILED! => {"changed": false, "error": "No module named 'kubernetes'", "msg": "Failed to import the required Python library (openshift) on localhost.localdomain's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

我已经尝试在机器和 openshift 上安装 ansible-galaxy kubernetes 模块。
不知道我做错了什么,因为这里有很多可能出错的地方。

Ansible 版本输出:
ansible 2.9.9
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/jcp/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/jcp/.local/lib/python3.6/site-packages/ansible
executable location = /home/jcp/.local/bin/ansible
python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]

我已经从 openshift 依赖项中调试了我的 python_required_info 输出,这就是我所拥有的:
ok: [localhost] => {
"openshift_dependencies": {
"changed": false,
"failed": false,
"mismatched": {},
"not_found": [],
"python": "/usr/bin/python3.6",
"python_system_path": [
"/tmp/ansible_python_requirements_info_payload_5_kb4a7s/ansible_python_requirements_info_payloa d.zip",
"/usr/lib64/python36.zip",
"/usr/lib64/python3.6",
"/usr/lib64/python3.6/lib-dynload",
"/home/jcp/.local/lib/python3.6/site-packages",
"/usr/local/lib/python3.6/site-packages",
"/usr/local/lib/python3.6/site-packages/openshift-0.10.0.dev1-py3.6.egg",
"/usr/lib64/python3.6/site-packages",
"/usr/lib/python3.6/site-packages"
],
"python_version": "3.6.8 (default, Nov 21 2019, 19:31:34) \n[GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]" ,
"valid": {
"openshift": {
"desired": null,
"installed": "0.10.0.dev1"
}
}
}
}

提前感谢您的帮助!

最佳答案

你不应该做任何特别的事情来使用模块 除了安装所需的 python 依赖项 .见:https://docs.ansible.com/ansible/latest/modules/k8s_info_module.html

  • 删除行 - role: ansible.kubernetes-modules ,除非它是您的模块,在这种情况下您必须告诉我们更多信息,因为这不是正确的声明。
  • 删除集合声明
  • 在使用模块之前在某处添加以下任务:

    - name: Make sure python deps are installed
    pip:
    name: openshift

    您的实际python_requirement_facts任务除了报告未找到依赖项之外什么都不做。注册结果并调试它以自己查看。

  • 现在使用 k8s_info模块正常。

    关于kubernetes - 如何使用 Kubectl 命令通过 Ansible 访问 Rancher 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62453794/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com