# Ansible-Vmware snapshot question

**URL:** <https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [December 30, 2019, 1:53pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886 "2019-12-30T13:53:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chr00ted](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chr00ted/32/2188_2.png) [@chr00ted](https://forum.ansible.com/u/chr00ted)\
**Post date:** [December 30, 2019, 1:53pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/1 "2019-12-30T13:53:01Z")

</div>

Hello all, I have been using Ansible to manage snapshots for single specified vm’s. Is there a way to have Ansible remove snapshots for all vm’s in Vmware datacenter? If this is posssible, would you mind providing a play book example?

Below is an example where I remove a snapshot from a single vm:

- name: VM Remove Snapshot  
hosts: localhost  
gather\_facts: false  
connection: local  
vars:  
vcenter\_hostname: vspherefqdn  
vcenter\_user: ansiblesusername  
vcenter\_pass: changeme  
esxhost: 192,168.1.100  
datastore: LUN01  
name: “{{ vm\_name }}”  
notes: Ansible Test  
dumpfacts: False  
tasks:
- name: Remove all snapshots  
vmware\_guest\_snapshot:  
validate\_certs: False  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_user }}”  
password: “{{ vcenter\_pass }}”  
datacenter: TST1  
name: “{{ name }}”  
folder: ‘’  
state: remove\_all  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![chr00ted](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chr00ted/32/2188_2.png) [@chr00ted](https://forum.ansible.com/u/chr00ted)\
**Post date:** [December 30, 2019, 3:42pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/2 "2019-12-30T15:42:47Z")

</div>

I’ve tried first running vmware\_vm\_info to get all vm’s and debug from that shows all vms allong with other info on the vm’s. I would like to loop the “guest\_name” into the above snapshot removal using with\_item, but not having any luck so far. I may be going at this the wrong way. Any help is appreciated,

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [December 30, 2019, 3:45pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/3 "2019-12-30T15:45:36Z")

</div>

Hi Gerry,

You may want to try this - Replace variables as per your setup

---

<div class="post-metadata">

**Author:** ![chr00ted](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chr00ted/32/2188_2.png) [@chr00ted](https://forum.ansible.com/u/chr00ted)\
**Post date:** [December 30, 2019, 4:26pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/4 "2019-12-30T16:26:57Z")

</div>

Thanks Abhijeet, I’m testing now. Its complaining mapping values are not allowed in this context

The offending line appears to be:

- name Get a list of all VM names  
set\_fact:  
^ here

Looks like it could be a spacing issue, but all spacing looks good. Still testing.

---

<div class="post-metadata">

**Author:** ![chr00ted](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chr00ted/32/2188_2.png) [@chr00ted](https://forum.ansible.com/u/chr00ted)\
**Post date:** [December 30, 2019, 4:52pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/5 "2019-12-30T16:52:07Z")

</div>

Abhijeet, that worked, thanks!. My next question is what if vcenter has multiple datacenters? I would like to do this for all datacenters.

---

<div class="post-metadata">

**Author:** ![chr00ted](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chr00ted/32/2188_2.png) [@chr00ted](https://forum.ansible.com/u/chr00ted)\
**Post date:** [December 30, 2019, 6:40pm UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/6 "2019-12-30T18:40:15Z")

</div>

Seems to be working on all datacenters (which is what I wanted) even though I only had one defined. Do you work @ Redhat? I was looking you up on LinkedIn.

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [December 31, 2019, 3:32am UTC](https://forum.ansible.com/t/ansible-vmware-snapshot-question/31886/7 "2019-12-31T03:32:59Z")

</div>

Hi Gerry,

vmware\_vm\_info finds all VMS irrespective of the datacenter (because it was designed that way) but snapshot takes datacenter and folder into consideration so I think you are good to go.

Yes, I work at RH.
