Delete a VMware Virtual Machine Snapshot - Ansible module vmware_guest_snapshot
How to automate the delete of snapshot named “Ansible Managed Snapshot” in a VMware Virtual Machine “myvm” Ansible Playbook and vmware_guest_snapshot module.


How to Delete 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 Delete a VMware Virtual Machine Snapshot
community.vmware.vmware_guest_snapshot
- Manages virtual machines snapshots in vCenter
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
- hostname string / username string / password string / datacenter string / validate_certs boolean - connection details
- state string - present / absent / revert / remove_all
- remove_children boolean - no/yes
- snapshot_name string description string - Name/description of the virtual machine to work with
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, “absent” to delete a snapshot. You could also manage 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
.
You need to specify the exact snapshot name that you would like to remove in the snapshot_name
parameter.
Links
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to Delete a VMware Virtual Machine Snapshot with Ansible. I’m going to show you how to Delete a snapshot of the Virtual Machine named “myvm” using Ansible Playbook named “Ansible Managed Snapshot” in your VMware vCenter.
code
- vm_snapshot_remove.yml
---
- name: vm snapshot demo
hosts: localhost
become: false
gather_facts: false
collections:
- community.vmware
pre_tasks:
- include_vars: vars.yml
tasks:
- name: remove snapshot
vmware_guest_snapshot:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ vcenter_datacenter }}"
validate_certs: "{{ vcenter_validate_certs }}"
name: "{{ vm_name }}"
folder: "{{ vm_folder }}"
snapshot_name: "Ansible Managed Snapshot"
state: absent
- vars.yml
---
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"
- inventory
localhost
execution
$ ansible-playbook vm_snapshot_remove.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 [remove 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_remove.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 [remove snapshot] ****************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
after execution
Recap
Now you know how to create a VMware vSphere Virtual Machine with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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
Donate
Want to keep this project going? Please donate