Season's greetings, all.
I've created a playbook to generate a report of all VMware guests (vms) which have snapshots:
---
- hosts: all
gather_facts: false
vars_prompt:
- name: "vcenter_username"
prompt: "Enter your Vcenter username"
private: no
- name: "vcenter_password"
prompt: "Enter your VMware password"
private: yes
#- name: "vcenter_hostname"
#prompt: "Enter your Vcenter Hostname\n1- vcenter1.mycompany.com\n2- vcenter2.mycompany.com\n"
#private: no
#- name: "vcenter_datacenter"
#prompt: "Enter your Vcenter Datacenter\n1- First Datacenter\n2- Second Datacenter\n"
#private: no
vars:
vcenter_hostname: vcenter1.mycompany.com
vcenter_datacenter: First Datacenter
tasks:
- name: Gather snapshot information about the virtual machines in the given vCenter
community.vmware.vmware_guest_snapshot_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ vcenter_datacenter }}"
folder: "/{{ vcenter_datacenter }}/vm"
validate_certs: False
name: "{{ inventory_hostname }}"
register: snapshot_info
delegate_to: localhost
- name: Show snapshots
ansible.builtin.debug:
msg: "{{ snapshot_info }}"
- local_action:
module: copy
content: |
{% for host in ansible_play_hosts %}
{{ host }}:
{{ hostvars[host]['snapshot_info']|default("None", false) }}
{% endfor -%}
dest: "/home/user/snapshotsB.txt"
run_once: yes
This works, producing the following output:
host-01:
{'changed': False, 'guest_snapshots': {}, 'failed': False}
host-02:
{'changed': False, 'guest_snapshots': {'snapshots': [{'id': 1355, 'name': 'VM Snapshot 12%2f20%2f2023, 08:01:11 PM', 'description': 'Pre OS patching - DGY', 'creation_time': '2023-12-21T01:01:12.319638+00:00', 'state': 'poweredOff', 'quiesced': False}], 'current_snapshot': {'id': 1355, 'name': 'VM Snapshot 12%2f20%2f2023, 08:01:11 PM', 'description': 'Pre OS patching - DGY', 'creation_time': '2023-12-21T01:01:12.319638+00:00', 'state': 'poweredOff', 'quiesced': False}}, 'failed': False}
host-01 has no snapshot, and I'd like only hosts that have snaps to be included in the report. How do I accomplish that?
Additionally, there are two datacenters, one on each of two vCenter hosts. You'll notice that I tried to create prompts to select the datacenter and host (currently commented out). While the prompts appear, and I choose the right values from the presented pick lists, it doesn't work. the errors are that vms are non-existent. How can I make this work?
Thanks so much.