Ansible-Vmware snapshot question

50 views
Skip to first unread message

Gerry Maddock

unread,
Dec 30, 2019, 8:53:01 AM12/30/19
to Ansible Project
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

Gerry Maddock

unread,
Dec 30, 2019, 10:42:47 AM12/30/19
to Ansible Project
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,

Abhijeet Kasurde

unread,
Dec 30, 2019, 10:46:09 AM12/30/19
to ansible...@googlegroups.com
Hi Gerry,

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

---
- hosts: localhost
vars_files:
- vcsim_vars.yml
tasks:
- name: Gather all VMs information
vmware_vm_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
register: all_vm_info
delegate_to: localhost

- name: Get a list of all VMs
set_fact:
all_vm: "{{ all_vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[*].guest_name"
- name: Delete all snapshot of all VMs
vmware_guest_snapshot:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
datacenter: '{{ datacenter }}'
name: '{{ item }}'
folder: "/{{ datacenter }}/vm"
state: remove_all
with_items: "{{ all_vm }}"
delegate_to: localhost



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/64304818-f49e-4151-927e-5b854d47e167%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Gerry Maddock

unread,
Dec 30, 2019, 11:26:58 AM12/30/19
to Ansible Project
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.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Gerry Maddock

unread,
Dec 30, 2019, 11:52:07 AM12/30/19
to Ansible Project
Abhijeet, that worked, thanks!. My next question is what if vcenter has multiple datacenters? I would like to do this for all datacenters.


On Monday, December 30, 2019 at 10:46:09 AM UTC-5, Abhijeet Kasurde wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Gerry Maddock

unread,
Dec 30, 2019, 1:40:15 PM12/30/19
to Ansible Project
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.


On Monday, December 30, 2019 at 10:46:09 AM UTC-5, Abhijeet Kasurde wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Abhijeet Kasurde

unread,
Dec 30, 2019, 10:33:32 PM12/30/19
to ansible...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a4cd2304-aaf3-41b3-8fd5-05df796862b6%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages