Why does an `"[Errno 2] No such file or directory", "rc"` issue occur when Ansible is executed remotely?

Running:

- name: get vhosts
  command: rabbitmqctl list_vhosts
  register: vhosts
  changed_when: false

by issuing

sudo ansible-playbook file.yml

results in:

TASK [030.sensu : get vhosts] **************************************************
fatal: [IP]: FAILED! => {"changed": false, "cmd": "rabbitmqctl list_vhosts", 
"failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}

while this error does not occur if ansible is run on the system itself.


Discussion

1. Is the rc command missing?

Issuing rc both locally as remotely results in:

bash: rc: command not found...

If this would be an issue then running the playbook locally would fail as well.


2. Is this a known issue?

Querying "No such file or directory" rc ansible centos on the internet resulted in this Q&A.

According to one of the answers the issue is caused by command. Using shell instead would solve the problem. The solution was tried, but it did not solve the issue.


The return from ansible is json. Pretty printed:

{
  "changed": false,
  "cmd": "rabbitmqctl list_vhosts",
  "failed": true,
  "msg": "[Errno 2] No such file or directory",
  "rc": 2
}

You'll note msg contains "[Errno 2] No such file or directory". Instead rc is the return (exit) code, '2' in your case, of the command rabbitmqctl list_vhosts.

In shell (bash/sh/whatever) run rabbitmqctl. You may not have rabbitmqctl installed. Or, as EEAA rightfully suggests, it is not in $PATH. On some distros (Debian/Ubuntu) /usr/sbin is not included in normal users' path, but it is included for root.


Adding become: true to the file.yml:

- hosts:
    - <enter-IP>
  roles:
    - 030.sensu
  become: true

and running it by issuing ansible-playbook file.yml solved the issue as well.