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

How to use Kubectl commands to Acess a Rancher Cluster through Ansible

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

---

- 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')}}"

问题是我每次尝试 运行 剧本时都会收到 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"
            }
        }
    }
}

提前感谢您的帮助!

编辑:以下答案是针对 OP 的特定 Ansible 版本(即 2.9.9)给出的,如果您仍然使用它仍然有效。从 2.10 版本开始,如果还没有安装相关的 ansible 集合

ansible-galaxy collection install kubernetes.core

有关详细信息,请参阅 the latest module documentation


在 Ansible 2.9.9 中,除了安装所需的 python 依赖项 外,您不应该做任何特殊的事情来使用模块 。参见 the module documentation for your Ansible version

  1. 删除行 - role: ansible.kubernetes-modules,除非它是您自己的模块,在这种情况下您必须告诉我们更多信息,因为这不是正确的声明。
  2. 删除集合声明
  3. 在使用模块之前在某处添加以下任务:
    - name: Make sure python deps are installed
      pip:
        name: openshift
    

您的实际 python_requirement_facts 任务只是报告未找到依赖项。注册结果并调试它自己看看。

现在可以正常使用k8s_info模块了。