严重错误:“ [Errno 2]没有这样的文件或目录”

时间:2020-01-14 12:22:44

标签: kubernetes ansible

我无法在ansible命令模块中执行kubectl(v1.16.3)命令。

例如使用ansible创建命名空间。

    tasks:
       - name: "Creating Directory"
         file:
           path: ~/ansible_ns/demo_namespaces
           state: directory

       - name: "Creating Namespaces(1/2)"
         copy:
           content: "apiVersion: v1 \nkind: Namespace \nmetadata: \n     name: {{item}} "
           dest: "~/ansible_ns/demo_namespaces/{{item}}.yml"
         with_items:
           - "{{ namespace }}"

       - name: "Creating Namespaces(2/2)"
         command: "kubectl create -f {{item}}.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml"
         args:
           chdir: ~/ansible_ns/demo_namespaces/
         ignore_errors: true
         with_items:
           - "{{ namespace }}"

我最终遇到以下错误:

(item=ns) => {
    "ansible_loop_var": "item",
    "changed": false,
    "cmd": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
    "invocation": {
        "module_args": {
            "_raw_params": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
            "_uses_shell": false,
            "argv": null,
            "chdir": "/root/ansible_ns/demo_namespaces/",
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "item": "ns",
    "msg": "[Errno 2] No such file or directory",
    "rc": 2
}

注意:但是我可以手动执行“ kubectl create -f ..”,它正在创建东西。

我的Ansible版本:

$ ansible --version
ansible 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/mdupaguntla/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

仅供参考,我也尝试使用Ansible-2.4.2。但是没有运气。

我的系统操作系统:CentOS 7

我的查询:

  1. 在我的上下文中,此错误是什么意思“ [Errno 2]没有这样的文件或目录”?

  2. 我知道Ansible引入了kubectl&k8s模块:社区中是否有人在使用这些模块。如果是,请让我知道如何使用它们。如果有任何先决条件,请与他们分享 对于kubectl模块:知道先决条件是kubectl go库。我可以知道在哪里 获取此库。

  3. 当kubectl版本为1.8而ansible版本为2.4.2时-我可以使用命令模块获取使用“ kubectl create -f ...”创建的K8s资源。但是,当我将群集从v1.8升级到v1.16.3时-我无法使用命令模块使用“ kubectl create -f ...”来创建资源。如果我错过了做事,让我来。

提前感谢社区

3 个答案:

答案 0 :(得分:3)

您必须在命令模块中添加kubectl的路径。

command: "/the/path/kubectl create -f {{item}}.yml .........."

这是因为$ PATH不会使用kubectl的路径进行更新。您也可以将路径添加到$ PATH中,而不是在命令模块中提供路径。

答案 1 :(得分:0)

嗯,有两种方法可以使此过程变得更好且更实用。

您可以尝试以这种方式使用k8s模块。

- name: Create k8s catota namespace
  k8s:
    name: catota
    api_version: v1
    kind: Namespace
    state: present

或者您也可以使用shell模块:

- name: Create k8s catota namespace
  shell: "kubectl create namespace catota"
  args:
    executable: /bin/bash

答案 2 :(得分:0)

如果您使用ansible 2.9.2,则它具有k8s模块可用。它提供了完全声明性的方法(与发出命令性命令相比),它与您在kubernetes本身中可以找到的更相似。

例如,如果要创建新的名称空间,请使用:

- name: Create a k8s namespace
  k8s:
    name: testing
    api_version: v1
    kind: Namespace
    state: present

您必须承认它看起来要简单得多。

相关问题