unable to run esxcli command with ansible

i got below pb

  • name: test

hosts: all

gather_facts: no

vars_files:

  • vcenter_creds.yml

  • vars.yml

tasks:

  • name: Enable SSH service

vmware_host_service_manager:

hostname: “{{ vcenter_hostname }}”

username: “{{ vcenter_sso_admin }}”

password: “{{ vcenter_sso_pass }}”

esxi_hostname: “{{ inventory_hostname }}”

validate_certs: no

state: present

service_name: “TSM-SSH”

delegate_to: localhost

  • name: Esxcli

shell:

cmd: “esxcli vm process list”

delegate_to: localhost

register: esxcli_output

  • name: Display esxcli output

debug:

var: esxcli_output.stdout_lines

but keep getting error:

fatal: [xxxxxxxxx → localhost]: FAILED! => {“changed”: true, “cmd”: “esxcli”, “delta”: “0:00:00.009652”, “end”: “2023-06-01 10:49:02.878893”, “msg”: “non-zero return code”, “rc”: 127, “start”: “2023-06-01 10:49:02.869241”, “stderr”: “/bin/sh: esxcli: command not found”, “stderr_lines”: [“/bin/sh: esxcli: command not found”], “stdout”: “”, “stdout_lines”: }

Are you running from the ESXi server? if not then you need to delegate tasks to the ESXi server which has esxcli.

Please see https://medium.com/@AbhijeetKasurde/ansible-automating-esxcli-using-shell-module-6f691a551bca

tl;dr -

my hosts file includes the esxi host

also i am able to run “df -h” without issues