ESXi upgrade using ansible playbook

Getting an error while patch the esxi host. Please help…

fatal: [localhost]: FAILED! => {“changed”: false, “cmd”: “esxcli software vib install -d /vmfs/volumes/datastore_10.115.81.40/VMware-ESXi-7.0U3n-21930508-depot.zip”, “msg”: “[Errno 2] No such file or directory: b’esxcli’”, “rc”: 2, “stderr”: “”, “stderr_lines”: , “stdout”: “”, “stdout_lines”: }


tasks:

  • name: ESXi enter maintenance
    register: status
    vmware_maintenancemode:
    hostname: “{{ vcenter_hostname }}”
    username: “{{ vcenter_username }}”
    password: “{{ vcenter_password }}”
    esxi_hostname: “{{ esxi_ip_address }}”
    #esxi_password: “{{ esxi_passwd }}”
    evacuate: yes
    validate_certs: no
    timeout: 3600
    state: present
    delegate_to: localhost

  • debug: var=status.msg

  • name: ESXi Install Update
    command: “esxcli software vib install -d /vmfs/volumes/datastore_10.115.81.40/VMware-ESXi-7.0U3n-21930508-depot.zip”

    register: vib

  • debug:
    msg: vib


This [localhost] means that the task is invoked to localhost instead of your ESXi. You should specify the target node of this task to your ESXi, by changing hosts on your playbook or delegate_to on the task.

i tried changing local host to my esxi host ip x.x.x.x but different error now

TASK [ESXi enter maintenance] ***************************************************************************************************************************************************
fatal: [localhost → x.x.x.x]: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: root@x.x.x.x: Permission denied (publickey,keyboard-interactive).”, “unreachable”: true}

tasks:

  • name: ESXi enter maintenance
    register: status
    vmware_maintenancemode:
    hostname: “{{ vcenter_hostname }}”
    username: “{{ vcenter_username }}”
    password: “{{ vcenter_password }}”
    esxi_hostname: “{{ esxi_ip_address }}”
    #esxi_password: “{{ esxi_passwd }}”
    evacuate: yes
    validate_certs: no
    timeout: 3600
    state: present
    delegate_to: x.x.x.x

  • debug: var=status.msg

  • name: ESXi Install Update
    command: “esxcli software vib install -d /vmfs/volumes/datastore_x.x.x.x/VMware-ESXi-7.0U3n-21930508-depot.zip”


The delegate_to: localhost for the task ESXi enter maintenance should not be changed. My comment is for the task ESXi Install Update.

Also you have to enable SSH service on ESXi.
To enable SSH service, you can do it by hand or use community.vmware.vmware_host_service_manager.

After SSH enable via play book but still below error.
i could see host going into MM and ssh also looks good.


TASK [ESXi Install Update] ******************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“changed”: false, “cmd”: “esxcli software vib install -d /vmfs/volumes/datastore_10.115.81.40/VMware-ESXi-7.0U3n-21930508-depot.zip”, “msg”: “[Errno 2] No such file or directory: b’esxcli’”, “rc”: 2, “stderr”: “”, “stderr_lines”: , “stdout”: “”, “stdout_lines”: }


Please find my play book mentioned below.,


  • hosts: localhost
    gather_facts: no

    vars:
    vcenter_hostname: “x.x.x.x”
    vcenter_username: “administrator@vsphere.local”
    vcenter_password: “xxxxxxxxxxxxxx”
    esxi_ip_address: “x.x.x.x”
    esxi_passwd: “xxxxxxxxxxxxxxxx”

    tasks:

    • name: ESXi enter maintenance
      register: status
      vmware_maintenancemode:
      hostname: “{{ vcenter_hostname }}”
      username: “{{ vcenter_username }}”
      password: “{{ vcenter_password }}”
      esxi_hostname: “{{ esxi_ip_address }}”
      #esxi_password: “{{ esxi_passwd }}”
      evacuate: yes
      validate_certs: no
      timeout: 3600
      state: present

      delegate_to: localhost

    • debug: var=status.msg

    • name: Enable TSM-SSH service
      register: status
      vmware_host_service_manager:
      hostname: “{{ vcenter_hostname }}”
      username: “{{ vcenter_username }}”
      password: “{{ vcenter_password }}”
      esxi_hostname: “{{ esxi_ip_address }}”
      validate_certs: no
      service_name: TSM-SSH
      state: present
      service_policy: off

      delegate_to: localhost

    • name: ESXi Install Update
      command: “esxcli software vib install -d /vmfs/volumes/datastore_x.x.x.x/VMware-ESXi-7.0U3n-21930508-depot.zip”

      register: vib

    • debug:
      msg: vib

Please read my comment above again :disappointed:

1 Like

The appropriate target node for each task depends on the modules used in the task.

The delegate_to is used when you want to change the target node of the task from the target nodes which specified by the hosts in the playbook.

1 Like

Nope still not working …getting below error

fatal: [localhost]: FAILED! => {“changed”: true, “cmd”: “/bin/esxcli software vib install -d /vmfs/volumes/datastore_x.x.x.x/VMware-ESXi-7.0U3n-21930508-depot.zip”, “delta”: “0:00:00.010569”, “end”: “2024-04-24 09:17:58.494133”, “msg”: “non-zero return code”, “rc”: 127, “start”: “2024-04-24 09:17:58.483564”, “stderr”: “/bin/sh: 1: /bin/esxcli: not found”, “stderr_lines”: [“/bin/sh: 1: /bin/esxcli: not found”], “stdout”: “”, “stdout_lines”: }

Hey have you read my comment? My English may have been poor, but I do not want to repeat the same comments any more.

hi…Please read the error once again …the error reports something different not for the localhost issue. I tired all the steps … If you have any idea please reply or else ignore.

msg": “[Errno 2] No such file or directory: b’esxcli’”, “rc”: 2, “stderr”: “”, “stderr_lines”: , “stdout”: “”, “stdout_lines”: }

From the beginning, before I care about the content of the error message, I see the problem with [localhost] next to fatal::

This means that your task is trying to run esxicli on localhost with SSH or local connection.
I don’t think this is correct for your task, and that’s why I repeatedly mentioned hosts and delegate_to.

If the No such file or directory error is truly came from ESXi, the line fatal should start with the ESXi hostname or IP address as follows:

fatal: [<ESXI_HOSTNAME_OR_IP_ADDRESS>]: FAILED! => {“changed”: true, ...

or if the task is delegated to ESXi,

fatal: [localhost -> <ESXI_HOSTNAME_OR_IP_ADDRESS>]: FAILED! => {“changed”: true, ...

I’ve started with ESX 2.5 nearly 20 years ago and I’m also maintaining community.vmware.

That said, we’re using this collection heavily at work. But I would never use it to update ESXi hosts. I don’t think Ansible is the right tool to do this. Use vCenter Lifecycle Manager for this.

If you want to run esxcli commands, maybe you should have a look at Get-EsxCli. We use PowerCLI a lot where Ansible doesn’t feel like the right tool (or community.vmware simply lacks the functionality we need).

Hi Mariolenz,
I know this is an older post…please bare with me. I wholehartedly agree about using vCenter Lifecycle Manager to update; however, any way to trigger the cluster remediation action from ansible? Im looking to integrate our ticketting system into ansible and essentially have the following process kick off:

  1. admin submits a scheduled change request
  2. on approval, connect to ansible aap and schedule cluster remediation
  3. collect failure output

I am very new to ansible and just now getting familiar with community.vmware module. I have an idea on how to complete what I want using PowerCLI but I would like to centralize automations into AAP.

Is this even possible or should i just go the PowerCLI route and just piece a hybrid of the two?

@vuser ATM it’s not possibly. I haven’t figured out yet how to control vCenter Lifecycle Manager stuff through the API, which I would need to implement this.

Anyway, when I do I think managing cluster images would be more important than triggering an update.

So if you want to have a quick solution, use another way like PowerCLI or something similar. I don’t know when we’ll be able to implement this.

BTW If you’re new to ansible and community.vmware, I suggest to also have a look at vmware.vmware and vmware.vmware_rest. I don’t think those collections have a solution for your problem, but they might provide some interesting modules to fix other problems you might run into.

Especially have a look at vmware.vmware since I’ve started to deprecate modules in community.vmware in favour of ones in this collection :wink:

1 Like

@mariolenz hey there! thank you for the reply. I did check out the other two but its as you said. By the way thank you for the work you and the others put into this. So far im loving the learning experience and currently tearing my hair out with the vmware_guest module but in all im enjoying it lol.

Im thinking I might have some luck just executing a powercli script from anisible. This will probably be a good Friday task.

Thanks again for the reply, much appreciated! take care.