Ansible Error: "[Errno 2] No such file or directory"

13,897

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.

Share:
13,897

Related videos on Youtube

manoj kumar
Author by

manoj kumar

Updated on June 04, 2022

Comments

  • manoj kumar
    manoj kumar almost 2 years

    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

    • Smily
      Smily over 4 years
      provide the path for kubectl before the command or try using the shell command or try to give become: true
    • manoj kumar
      manoj kumar over 4 years
      1. If I use shell module. I'm getting "/bin/sh: kubectl: command not found". 2. I have already used below things become: true become_method: sudo become_user: root 3. "provide the path for kubectl before the command" - like? - any example?
    • Smily
      Smily over 4 years
      I mean path for kubectl. command: "/the/path/kubectl create -f {{item}}.yml. the path where kubectl exists
    • manoj kumar
      manoj kumar over 4 years
      @smily - it's working.. thanks a lot... But why I should I place the path of kubectl in the command. Is there any workaround for that?
  • manoj kumar
    manoj kumar over 4 years
    If I use shell module. I'm getting "/bin/sh: kubectl: command not found"
  • manoj kumar
    manoj kumar over 4 years
    While using k8s Module: I'm getting this error "Failed to import the required Python library (openshift) on abc059.xyz.local's(Hostname of my VM) Python /usr/bin/python. 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". Is openshift need to be installed explictly at the path /usr/bin/python?- I'm new to this.
  • manoj kumar
    manoj kumar over 4 years
    While using k8s Module: I'm getting this error"Failed to import the required Python library (openshift) on abc059.xyz.local's(Hostname of my VM) Python /usr/bin/python. 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". Is openshift need to be installed explictly at the path /usr/bin/python?- I'm new to this
  • mario
    mario over 4 years
    Your python interpreter needs additional library openshift ( pypi.org/project/openshift ) to be able to handle operations required by k8s module. The easiest way to install it is by using your python package manager pip. You just need to run this command: pip install openshift.
  • Daniel Marques
    Daniel Marques over 4 years
    It seems a lot of people are facing the same error as yours. Looking for some issues on GitHub I came across one that might helps you: github.com/ansible/ansible/issues/50529#issuecomment-4513408‌​90. About the shell module, it looks like your kubectl is not added on your $PATH in order to /bin/sh to used it. Can you execute kubectl create ns namespace-name manually on the host where you intend to run those ansible commands?
  • Daniel Marques
    Daniel Marques over 4 years
    The shell module uses /bin/sh by default to run commands. Try to set bash shell instead. I edited my answer, check it out.
  • manoj kumar
    manoj kumar over 4 years
    My Kubectl is located in the path /usr/local/bin echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin‌​:/root/bin FYI, the kubectl path already there in the $PATH
  • manoj kumar
    manoj kumar over 4 years
    I got a solution from smily. I'm going with that solution as I have to do minimal changes in my source code. I'll also try to use k8s module some other day and will let u know.. Thanks a lot for ur help as well..
  • mario
    mario over 4 years
    I totally understand that you may prefer to use your current solution as its preparation required quite a lot of time and energy and it is practically ready. However, keep in mind that this is not a very flexible solution. Note that the existence of shell and command modules in Ansible is a kind of workaround for doing things that cannot be properly done with existing modules. Running imperative commands in your playbooks is contrary to the idea of idempotency, one of the core concepts present in Ansible.
  • Smily
    Smily over 4 years
    May be the particular user's path variable doesnt have $PATH set with this. Try to echo $PATH from Ansible to verify that
  • Smily
    Smily over 4 years
    shell: "echo $PATH"
  • manoj kumar
    manoj kumar over 4 years
    Output of echo $PATH via Ansible: "stdout_lines": ["/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:..‌​......."]. It's having /usr/local/bin.
  • Smily
    Smily over 4 years
    Oh then am sorry, that was my understanding, but I hope giving the path will fix your issue
  • manoj kumar
    manoj kumar over 4 years
    ok will check my ansible configurations. If I find any.. will update here. Thanks for the responses and help.