Add and Manage ESXi Hosts in vCenter using Ansible Automation

Ansible is a powerful automation tool widely used for configuration management, application deployment, and task automation. In the context of VMware environments, Ansible can help simplify the process of managing ESXi hosts and integrating them into a vCenter server. By using Ansible’s community.vmware.vmware_host module, system administrators can automate the addition, reconnection, and configuration of ESXi hosts within a vCenter environment.

In this article, we will explore the key components of an Ansible Playbook that adds ESXi hosts to vCenter and manage various host configurations. We will also cover different scenarios such as adding hosts to specific clusters, folders, and using SSL thumbprints for secure communications.

Key Playbook Tasks Explained

1. Add ESXi Host to vCenter

To add an ESXi host to a vCenter, you can use the following Ansible Playbook task. This task ensures that the specified ESXi host is added to the vCenter under a specific datacenter and cluster.

- name: Add ESXi Host to vCenter
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: present
  delegate_to: localhost

Explanation:

  • hostname: The address of the vCenter server.
  • username and password: The credentials used to authenticate with vCenter.
  • datacenter and cluster: Specifies the datacenter and cluster where the ESXi host will be added.
  • esxi_hostname, esxi_username, esxi_password: The ESXi host’s credentials for connection.
  • state: The state is set to present to ensure that the host is added.

This task is useful when you need to automate the deployment of multiple ESXi hosts into a vCenter environment without manual intervention.

2. Add ESXi Host to vCenter under a Specific Folder

If you want to organize your hosts into specific folders in vCenter, this task will help you add an ESXi host to a designated folder within the vCenter infrastructure.

- name: Add ESXi Host to vCenter under a specific folder
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    folder: '/Site2/Asia-Cluster/host'
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: present
    add_connected: true
  delegate_to: localhost

Explanation:

  • folder: Specifies the folder where the ESXi host will be added. In this example, the path /Site2/Asia-Cluster/host defines the folder structure in vCenter.
  • add_connected: When set to true, the host will be added only if it’s connected to vCenter.

3. Reconnect ESXi Host (with Username/Password)

In some situations, an ESXi host may lose connection to the vCenter server. This task allows you to reconnect the host by specifying its credentials.

- name: Reconnect ESXi Host (with username/password set)
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    state: reconnect
  delegate_to: localhost

Explanation:

  • state: The state is set to reconnect to re-establish the connection between the ESXi host and vCenter.
  • esxi_username and esxi_password: Credentials are required to reconnect the ESXi host to vCenter.

4. Reconnect ESXi Host (with Default Username/Password)

If you are using the default credentials for your ESXi host (like the root user), this task can help you reconnect the ESXi host to vCenter.

- name: Reconnect ESXi Host (with default username/password)
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    state: reconnect
  delegate_to: localhost

Explanation:

  • state: The state is again reconnect, but no custom username/password are provided, assuming default credentials for ESXi.

5. Add ESXi Host with SSL Thumbprint

For enhanced security, you might need to verify the SSL thumbprint when connecting an ESXi host to vCenter. This task demonstrates how to add an ESXi host with the SSL thumbprint.

- name: Add ESXi Host with SSL Thumbprint to vCenter
  community.vmware.vmware_host:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: datacenter_name
    cluster: cluster_name
    esxi_hostname: '{{ esxi_hostname }}'
    esxi_username: '{{ esxi_username }}'
    esxi_password: '{{ esxi_password }}'
    esxi_ssl_thumbprint: "3C:A5:60:6F:7A:B7:C4:6C:48:28:3D:2F:A5:EC:A3:58:13:88:F6:DD"
    state: present
  delegate_to: localhost

Explanation:

  • esxi_ssl_thumbprint: This is a unique string representing the SSL thumbprint of the ESXi host’s certificate. By verifying the thumbprint, you can ensure secure communication between the ESXi host and vCenter.

Ansible’s integration with VMware via the community.vmware.vmware_host module allows system administrators to efficiently automate the management of ESXi hosts in vCenter environments. Whether you need to add hosts, reconnect them, or organize them into specific folders, Ansible Playbooks provide an easy, repeatable solution for managing large VMware infrastructures.

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply