Using Ansible Playbook To Snapshot Virtual Machine On vSphere

Using Ansible Playbook To Snapshot Virtual Machine On vSphere

Posted by vmt1991 on 17 Mar 2021
Linux-Unix

Using Ansible Playbook To Snapshot Virtual Machine On vSphere

- Ansible provides various modules to manage VMware infrastructure, which includes datacenter, cluster, host system and virtual machine.

Deploy VMware virtual machines with Ansible - Zwindler's Reflection

I/Requirements:

- Ansible VMware modules are written on top of pyVmomi. Install  pyVmomi python module first:

# pip3 install pyvmomi

- Ansible VMware modules leveraging latest vSphere(6.0+) features are using vSphere Automation Python SDK. Install vSphere Automation Python SDK using pip:

# git clone https://github.com/vmware/vsphere-automation-sdk-python.git

# cd vsphere-automation-sdk-python

# mkdir /root/ansible/vmware_sdk_lib

# pip3 install -r requirements.txt --extra-index-url /source/ansible_vmware/vsphere-automation-sdk-python/lib

II/Installing SSL Certificates:

1/Install vCenter SSL Certificate To Ansible Host:

- Go to the base URL of the vCenter Server: https://IPVcenter

- Click the “Download trusted root CA certificates” >> Zip file

- Extract the contents of the zip file > upload certs folder to Ansible Host

2/Installing ESXi SSL certificates for Ansible:

- SSH to ESXi server using administrative credentials, and navigate to directory /etc/vmware/ssl

- Download rui.crt and upload to Ansible host at location: /etc/pki/ca-trust/source/anchors/

III/Using Playbook To Snapshot VM Before Processing Update:

- Prepare ansible configuration file with static inventory to localhost

- Create secret for storing variables username and password for accessing vcenter

-Writing playbook execute task as workflow:

Shutdown guest VM->Snapshot VM ->Starting VM

- name: Snapshot Workflow For VM Before Update OS

  hosts: localhost

  become: true

  vars:

   snap_name: Before_Update_17032021

  vars_files:

   - list_vms.txt

  tasks:

   - name: Load username and password variable for vCenter access

     include_vars:

      file: /root/ansible/books/vmware/password.yml

   - name: Print username and password to test

     debug:

      msg: "{{ username + '-' + password}}"

   - name: Shutdown Guest VM

     vmware_guest_powerstate:

       hostname: 192.168.9.99

       username: "{{username}}"

       password: "{{password}}"

       state: shutdown-guest

       name: "{{item}}"

     loop: "{{vm_snapshot}}"

   - name: Pause Playbook run 1 minute for shutdown all VM complete

     pause:

      minutes: 1

   - name: Snapshot VM after shutdown OS

     vmware_guest_snapshot:

       hostname: 192.168.9.99

       username: "{{username}}"

       password: "{{password}}"

       datacenter: DC-TestLab

       folder: DC-TestLab/vm

       snapshot_name: Before_Update_17032021

       name: "{{item}}"

     loop: "{{vm_snapshot}}"

   - name: Start VM after snapshot process

     vmware_guest_powerstate:

       hostname: 192.168.9.99

       username: "{{username}}"

       password: "{{password}}"

       state: powered-on

       name: "{{item}}"

     loop: "{{vm_snapshot}}"

- Run playbook to process task as workflow:

- Checking all VM startup and has snapshot