skip to Main Content

I’m not able to execute kubectl(v1.16.3) commands in the ansible command module.

For e.g. Creation of Namespace using 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 }}"

I’m ending up with the below error:

(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
}

NOTE: But I’m able to do “kubectl create -f ..” manually..and it is creating the stuff.

My Ansible version:

$ 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)]

FYI, I also tried with Ansible – 2.4.2 as well. But No luck.

My System OS: CentOS 7

My queries:

  1. What is this error mean “[Errno 2] No such file or directory” in my context?

  2. I came to know that Ansible introduced kubectl & k8s module: Is there anyone in the community using these.. If Yes, please let me know how to use them. If they any prerequisites – please share them
    For kubectl Module: Came to know that the pre-requisite is kubectl go library.May I know where can I
    get this Library.

  3. when the kubectl version is 1.8 and ansible version is 2.4.2 – I’m able to get the K8s resources created using “kubectl create -f …” using command module. But when I upgraded my cluster from v1.8 to v1.16.3 – I’m not able to create the resources using “kubectl create -f …” using command module. Let me if I missed doing things.

Thanks in advance for the Community

4

Answers


  1. Well, there are two ways to make this process better and functional.

    You can try to use k8s module like this way.

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

    Or you can use the shell module as well:

    - name: Create k8s catota namespace
      shell: "kubectl create namespace catota"
      args:
        executable: /bin/bash
    
    Login or Signup to reply.
  2. If you use ansible 2.9.2, it has k8s module available. It provides fully declarative approach ( versus issuing imperative commands ) which is more similar to what you can find in kubernetes itself.

    For example if you want to create a new namespace, just use:

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

    You have to admit it looks much simpler.

    Login or Signup to reply.
  3. You have to add the path for kubectl in the command module.

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

    This is because the $PATH is not updated with the path of kubectl. You can add the path to $PATH also instead of giving the path in command module.

    Login or Signup to reply.
  4. Troubleshooting This Issue

    The Error:

    (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
    }
    

    First, notice that this states "_uses_shell": false,. This is because it is using Command instead of shell. This is also disguising the error code. If we switch to shell and re-run we get:

        "msg": "non-zero return code",
        "rc": 127,
        "start": "2021-09-03 13:48:12.184639",
        "stderr": "/bin/bash: <PROGRAM>: command not found",
    

    (details on exit codes https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html )

    The /bin/bash is the giveaway. Doing which kubectl you might get something like /usr/local/bin/kubectl.

    You either need to update the ansible command to use /usr/local/bin/kubectl or update the $PATH for /bin/bash/ to find it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search