Ansible Pilot

Take a VMware Virtual Machine Snapshot - Ansible module vmware_guest_snapshot

How to automate taking snapshots named “Ansible Managed Snapshot” to a VMware Virtual Machine “myvm” Ansible Playbook and vmware_guest_snapshot module.

May 18, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to Take a VMware Virtual Machine Snapshot with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible Take a VMware Virtual Machine Snapshot

Let’s talk about the Ansible module vmware_guest_snapshot. The full name is community.vmware.vmware_guest_snapshot, which means that is part of the collection of modules to interact with VMware, community-supported. It manages virtual machine snapshots in vCenter.

Parameters

The following parameters are useful in order to Take a VMware Virtual Machine Snapshot using the module vmware_guest_snapshot. First of all, we need to establish the connection with VMware vSphere or VMware vCenter using a plethora of self-explicative parameters: hostname, username, password, datacenter, and validate_certs. Once the connection is successfully established you could specify the desired snapshot state, in this case, “present” to take a snapshot. You could also revert or remove a snapshot with the same Ansible module. If you want to remove a snapshot you could also remove all the dependent snapshots using the parameter remove_children. It’s a good practice to set the name and description of the snapshot using thesnapshot_name and description parameters. Advanced practice is to create the memory dump of the virtual machines, please note that memory snapshots take time and resources will take a longer time to create. Default memory dump is disabled but you could enable using memory_dump parameter.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

How to Take a VMware Virtual Machine Snapshot with Ansible. I’m going to show you how to take a snapshot of the Virtual Machine named “myvm” using Ansible Playbook setting a name and description to easily find in your VMware vCenter.

code

---
- name: vm snapshot demo
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: create snapshot
      vmware_guest_snapshot:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        name: "{{ vm_name }}"
        state: present
        snapshot_name: "Ansible Managed Snapshot"
        folder: "{{ vm_folder }}"
        description: "This snapshot is created by Ansible Playbook"
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vm_template: "mytemplate"
vm_folder: "myvm"
localhost

execution

$ ssh [email protected]
[devops@demo ~]$ cd vmware/
[devops@demo vmware]$ ls
inventory         vars.yml       vm_deploy_template.yml  vm_snapshot_create.yml  vm_stop.yml
requirements.yml  vm_create.yml  vm_info.yml             vm_start.yml
[devops@demo vmware]$ ansible-playbook vm_snapshot_create.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [vm snapshot demo] ***************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [create snapshot] ****************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

idempotency

$ ansible-playbook vm_snapshot_create.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [vm snapshot demo] ***************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [create snapshot] ****************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
before execution
after execution

before execution

vmware_guest_snapshot before execution

after execution

vmware_guest_snapshot after execution

code with ❤️ in GitHub

Recap

Now you know how to create a VMware vSphere Virtual Machine with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases