繁体   English   中英

如何使用 Ansible-K8s 在 k8s 中创建秘密

[英]How to create a secret in k8s using Ansible-K8s

我在我的 ansible playbook 中创建了以下任务。

- name: Create a k8s namespace
      k8s: 
        state: present
        definition:
          apiVersion: v1
          kind: Secret
          metadata:
            name: bigip-login
            namespace: kube-system
          data:
            password: dGVzdA==
            username: YWRtaW4=
          type: Opaque

但是,当我运行剧本时,出现以下错误:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_k8s_payload_n071fcyu/ansible_k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py", line 92, in <module>
    from kubernetes.dynamic.resource import ResourceInstance
ModuleNotFoundError: No module named 'kubernetes'
fatal: [master.madebeen.com]: FAILED! => {
    "changed": false,
    "error": "No module named 'kubernetes'",
    "invocation": {
        "module_args": {
            "api_key": null,
            "api_version": "v1",
            "append_hash": false,
            "apply": false,
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "context": null,
            "continue_on_error": false,
            "definition": {
                "apiVersion": "v1",
                "data": {
                    "password": "VGFyLk1pZC5GdW4tNDU2",
                    "username": "YWRtaW4="
                },
                "kind": "Secret",
                "metadata": {
                    "name": "bigip-login",
                    "namespace": "kube-system"
                },
                "type": "Opaque"
            },
            "delete_options": null,
            "force": false,
            "host": null,
            "kind": null,
            "kubeconfig": null,
            "label_selectors": null,
            "merge_type": null,
            "name": null,
            "namespace": null,
            "password": null,
            "persist_config": null,
            "proxy": null,
            "proxy_headers": null,
            "resource_definition": {
                "apiVersion": "v1",
                "data": {
                    "password": "VGFyLk1pZC5GdW4tNDU2",
                    "username": "YWRtaW4="
                },
                "kind": "Secret",
                "metadata": {
                    "name": "bigip-login",
                    "namespace": "kube-system"
                },
                "type": "Opaque"
            },
            "src": null,
            "state": "present",
            "template": null,
            "username": null,
            "validate": null,
            "validate_certs": null,
            "wait": false,
            "wait_condition": null,
            "wait_sleep": 5,
            "wait_timeout": 120
        }
    },
    "msg": "Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3. Please read the module documentation and install it 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"

根据此处提供的示例如何使用应该可以工作的Ansible-K8s 模块在 k8s 中创建秘密 由于这里没有提供 json 文件作为示例,我还尝试了以下建议(没有任何成功) 如何使用 Ansible 创建 Kubernetes Secret?

---
  apiVersion: v1
  data:
    password: dGVzdA==
    username: YWRtaW4=
  kind: Secret
  metadata:
    name: bigip-login
    namespace: kube-system
  type: Opaque

令我感兴趣的是,当前安装了两个社区/核心 kubernetes 版本。

marlon@ansible:~/.ansible$ ansible-galaxy collection install community.kubernetes
Process install dependency map
Starting collection install process
Skipping 'community.kubernetes' as it is already installed
marlon@ansible:~/.ansible$ ansible-galaxy collection install kubernetes.core
Process install dependency map
Starting collection install process
Skipping 'kubernetes.core' as it is already installed
marlon@ansible:~/.ansible$

这是 ansible 当前使用的我的 python 版本。

    marlon@ansible:~$ python3 --version
Python 3.8.10
marlon@ansible:~$ ansible --version | grep "python version"
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
marlon@ansible:~$

像 ansible 安装文件中推荐的那样安装 ubuntu https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-ubuntu

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

您是否对用例 1 和 2 有任何建议,以便我们可以一劳永逸地将其留在这里以供将来参考以供其他人从中受益?

这个错误

"Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3.

意味着您没有安装 kubernetes 模块。 通常你可以通过执行命令来解决这个问题

pip3 install kubernetes

但是,您使用的是 ansible,因此您将不得不采取不同的方法。 尝试将此依赖项添加到您的系统映像中。 一个类似的问题已经被问过here 问题出在不同的模块上,但程序对您来说也是相同的。 您可以在此处找到示例系统映像定义。 (注意,这个人使用 Python 2 而你的版本是 Python 3)。

在你的情况下,你必须把命令

pip3 install kubernetes

在您的系统映像定义中。 如果您使用的是基本系统映像,请尝试通过添加上述行来创建您的自定义。 在 Ansible 可以使用此 Python 依赖项之前,应将其编码并安装到图像中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM