Ansible Playbook: ESXi Maintenance Mode and Reboot

In Ansible, you can run multiple tasks by grouping them under a single play. A play is a collection of tasks that target a specific set of hosts and are executed in order. In this article I would like to share couple of my Ansible Playbooks for ESXi Maintenance and Reboot.

All Ansible Playbooks used in this article can be found at My GitHub Repository


Prerequisites

Before using these Playbooks, please get familiar with my other articles about how to prepare Ansible in your infrastructure:

How to manage and run Ansible Playbooks remotely

How to deploy Ansible to manage VMware infrastructure

Configure Ansible Inventory

In this Playbook I execute tasks in vCenter by connecting to the vCenter Server from localhost (Ansible VM) using administrator credentials. Make sure you have Ansible Inventory configured.

My Ansible Inventory contains the following:

[localhost] – this is my Ansible VM from which I execute tasks in vCenter by connecting to the vCenter Server with administrator credentials.

In the article below you have a step by step guide how to configure Ansible Inventory:

What is Ansible Inventory and How to Add ESXi

Add ESXi to hosts file

Before using the ESXi Maintenance Mode and Reboot Ansible Playbook shown below, ESXi must be added to the hosts file on the Ansible node. This is because the Playbook contains a task called “ESXi wait for the reboot to complete”, which requires a connection by hostname. I have already tested that, if your ESXi is not added to the hosts file, the job will not complete, it will fail after a timeout.

Follow the instructions to add ESXi to the hosts file on the Ansible node:

1. To edit hosts file, run the following command:

sudo nano /etc/hosts

2. Add your ESXi IP and hostname. Once added, press Ctrl+O to Save the file, then Enter and then Ctrl+X to exit.


ESXi Maintenance Mode

Let’s start with two simple playbooks to Enter and Exit Maintenance Mode on ESXi.

First playbook is to Enter Maintenance Mode on ESXi.

---
- name: Enter MM on ESXi host #vmcloud.pl
  hosts: localhost
  gather_facts: no

  vars:
    vcenter_server: "vCenter_IP_or_hostname"
    vcenter_username: "administrator@vsphere.local"
    vcenter_password: "pw"
    esxi_hostname: "ESXi_hostname"

  tasks:
    - name: Enter ESXi Maintenance Mode
      register: status
      community.vmware.vmware_maintenancemode:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        evacuate: false   #Value true will migrate Powered Off VMs to other host
        timeout: 3600     #Timeout for the operation (sec)
        state: present

    - debug: var=status.msg

Run playbook using the following command:

ansible-playbook Enter_MM_ESXi.yml

Result:

Now, lets Exit Maintenance Mode on this ESXi using Ansible.

---
- name: Exit MM on ESXi host #vmcloud.pl
  hosts: localhost
  gather_facts: no

  vars:
    vcenter_server: "vCenter_IP_or_hostname"
    vcenter_username: "administrator@vsphere.local"
    vcenter_password: "pw"
    esxi_hostname: "ESXi_hostname"

  tasks:
    - name: Exit ESXi Maintenance Mode
      register: status
      community.vmware.vmware_maintenancemode:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        timeout: 3600     #Timeout for the operation (sec)
        state: absent

    - debug: var=status.msg

Run playbook using the following command:

ansible-playbook Exit_MM_ESXi.yml

Result:


ESXi Maintenance and Reboot

Now, lets combine Maintenance Mode with Reboot. This playbook is really helpful, because within one click you have 4 tasks in order:

  • Enter Maintenance Mode
  • Reboot
  • Wait for the reboot to complete
  • Exit Maintenance Mode
---
- name: ESXi Reboot with MM #vmcloud.pl
  hosts: localhost
  gather_facts: no

  vars:
    vcenter_server: "vCenter_IP_or_hostname"
    vcenter_username: "administrator@vsphere.local"
    vcenter_password: "pw"
    esxi_hostname: "ESXi_hostname"

  tasks:
    - name: Enter ESXi Maintenance Mode
      register: status
      community.vmware.vmware_maintenancemode:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        evacuate: false   #Value true will migrate Powered Off VMs to other host
        timeout: 3600     #Timeout for the operation (sec)
        state: present

    - debug: var=status.msg

    - name: ESXi reboot 
      vmware_host_powerstate: 
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        timeout: 3600   #Timeout for the operation (sec)
        state: reboot-host
      register: reboot_host
  
    - name: ESXi wait for the reboot to complete
      wait_for:
        host: "{{ esxi_hostname }}"
        port: 443
        delay: 600        #Perform first check after delay (sec)
        state: started
        timeout: 3600     #Timeout for the operation (sec)

    - name: Exit ESXi Maintenance Mode
      register: status
      community.vmware.vmware_maintenancemode:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        timeout: 3600     #Timeout for the operation (sec)
        state: absent

    - debug: var=status.msg

Run playbook using the following command:

ansible-playbook ESXi_Reboot_with_MM.yml

Results:

And the last playbook in this article, just for ESXi reboot without MM. I do not recommend rebooting ESXi host without enabled Maintenance Mode, especially in Production environment with running VMs. So, this playbook is just for testing purposes.

---
- name: ESXi Reboot #vmcloud.pl
  hosts: localhost
  gather_facts: no

  vars:
    vcenter_server: "vCenter_IP_or_hostname"
    vcenter_username: "administrator@vsphere.local"
    vcenter_password: "pw"
    esxi_hostname: "ESXi_hostname"

  tasks:
    - name: ESXi reboot 
      vmware_host_powerstate: 
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        validate_certs: no
        timeout: 3600   #Timeout for the operation (sec)
        state: reboot-host
        force: true     #This is required to forcely reboot ESXi host when it is not in Maintenance Mode
      register: reboot_host
  
    - name: ESXi wait for the reboot to complete
      wait_for:
        host: "{{ esxi_hostname }}"
        port: 443
        delay: 360        #Perform first check after delay (sec)
        state: started
        timeout: 3600     #Timeout for the operation (sec)

Run playbook by using the following command:

ansible-playbook ESXi_reboot.yml

Results:

As you can see, Ansible is a really powerful tool. You can perform many tasks with one click.


Thank you for reading! Here are some links you may be interested in:

Leave a comment

Create a website or blog at WordPress.com

Up ↑