Playbook for VMware help

107 views
Skip to first unread message

Dimitri Yioulos

unread,
Dec 22, 2023, 8:43:29 AM12/22/23
to Ansible Project
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.

Todd Lewis

unread,
Dec 22, 2023, 10:48:43 AM12/22/23
to ansible...@googlegroups.com, uto...@gmail.com
I'm not sure about your prompt question,
but for the report, try this:
        content: |
          {% for host in ansible_play_hosts %}
          {%     if hostvars[host].snapshot_info.guest_snapshots.snapshots is defined %}
          {{ host }}:
          {{ hostvars[host]['snapshot_info']|default("None", false) }}

          {%     endif %}
          {% endfor -%}
Cheers,
--
Todd
--
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/d5cae40b-0a06-4e54-81f7-b4a297207799n%40googlegroups.com.

Dimitri Yioulos

unread,
Dec 22, 2023, 11:00:08 AM12/22/23
to Ansible Project
Todd, that worked. I consider it a great Christmas present. Thanks! And, have a great Christmas!

If anyone can help me solve the prompt issue, I'd appreciate that, too.

Reply all
Reply to author
Forward
0 new messages