Jiu hong
Jiu hong

Reputation: 139

ansible shell: command -v xxx => command not found

I am using ansible to check if nvm exists or not. However, the result shows "rc": 127

  tasks:

    - name: "change sh to bash and source ~/.bashrc"
      shell: 
        cmd: source {{ ansible_env.HOME }}/.bashrc
      args:
        executable: /bin/bash

    - name: "Check if nvm exists"
      shell: command -v nvm
      register: result

result

fatal: [server3]: FAILED! => {"changed": true, "cmd": "command -v nvm", "delta": "0:00:00.001633", "end": "2022-08-08 00:48:12.763898", "msg": "non-zero return code", "rc": 127, "start": "2022-08-08 00:48:12.762265", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

The nvm does exist on the server

$ nvm -v
0.39.1

What is the reason please? Thank you in advance.

Upvotes: 0

Views: 825

Answers (1)

Shaik Saddam Hussain
Shaik Saddam Hussain

Reputation: 69

Below is man page of "command" - Below shows how "command" works.

command [-pVv] command [arg ...]
              Run command with args suppressing the normal shell function lookup. Only builtin commands or commands found in the PATH are executed.  If the -p option is given, the search
              for  command is performed using a default value for PATH that is guaranteed to find all of the standard utilities.  If either the -V or -v option is supplied, a description
              of command is printed.  The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option produces a more verbose
              description.   If  the -V or -v option is supplied, the exit status is 0 if command was found, and 1 if not.  If neither option is supplied and an error occurred or command
              cannot be found, the exit status is 127.  Otherwise, the exit status of the command builtin is the exit status of command. 

Now lets understand the issue that you reported: You have got rc 127, it means "command" could not find "nvm"(in your case) in default system PATH.

For troubleshoot, you need to identify suitable method to install nvm - For example, if you are installing nvm using package manager ansible modules (like yum, apt) mostly PATH is updated accordingly.

If you are trying to install nvm from tarball or source code then you need to add additional task to setup PATH variable before running task for "command -v nvm" so that PATH variable is updated before "command" is executed and you will get desired output.

Upvotes: 1

Related Questions