首页 > 解决方案 > 使用 k8s 事实或 K8s 信息模块的 ansible 错误

问题描述

团队,

我正在尝试使用 k8s_facts 模块中的标签拉节点,但收到此错误。任何提示?

任务/main.yaml

#Pull all cpu nodes by class label then check if that node has nodeType label
      - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
        k8s_facts:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: cpu_class_list == ''

输出:

16:00:16  ok: [localhost] => {
16:00:16      "changed": false, 
16:00:16      "failed_when_result": false, 
16:00:16      "module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 114, in <module>\n  File \"<stdin>\", line 106, in _ansiballz_main\n  File \"<stdin>\", line 49, in invoke_module\n  File \"/tmp/ansible_k8s_facts_payload_9r6jpU/__main__.py\", line 176, in <module>\n  File \"/tmp/ansible_k8s_facts_payload_9r6jpU/__main__.py\", line 172, in main\n  File \"/tmp/ansible_k8s_facts_payload_9r6jpU/__main__.py\", line 145, in execute_module\n  File \"/tmp/ansible_k8s_facts_payload_9r6jpU/ansible_k8s_facts_payload.zip/ansible/module_utils/k8s/common.py\", line 186, in get_api_client\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 645, in load_kube_config\n    persist_config=persist_config)\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 613, in _get_kube_config_loader_for_yaml_file\n    **kwargs)\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 153, in __init__\n    self.set_active_context(active_context)\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 173, in set_active_context\n    context_name = self._config['current-context']\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 495, in __getitem__\n    v = self.safe_get(key)\n  File \"/usr/local/lib/python2.7/dist-packages/kubernetes/config/kube_config.py\", line 491, in safe_get\n    key in self.value):\nTypeError: argument of type 'NoneType' is not iterable\n", 
16:00:16      "module_stdout": "", 
16:00:16      "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
16:00:16      "rc": 1
16:00:16  }

我已手动验证我的直接 kubectl 命令是否有效。所以它肯定是模块的一些设置问题?

当我切换到 k8s_info 模块时,出现语法错误。

      - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
        k8s_info:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: cpu_class_list == ''

输出

16:14:22  ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
16:14:22  
16:14:22  The error appears to be in '/home/svcngcc/jenkins/workspace/nvdc/run_ansible_playbook/k8s/baremetal/roles/maglev-services-pre-install-checks/tasks/main.yml': line 72, column 9, but may
16:14:22  be elsewhere in the file depending on the exact syntax problem.
16:14:22  
16:14:22  The offending line appears to be:
16:14:22  
16:14:22  #Pull all cpu nodes by class label then check if that node has nodeType label
16:14:22        - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
16:14:22          ^ here

标签: ansibleansible-2.xansible-facts

解决方案


我必须通过 ansible 容器内的 pip 安装 kubernetes 并且它有效。

pip install openshift pip install kubernetes


推荐阅读