Skip to content

Instantly share code, notes, and snippets.

@bschonec
Last active September 28, 2022 12:13
Show Gist options
  • Save bschonec/b1fa89420e601d565b058b5dc4010517 to your computer and use it in GitHub Desktop.
Save bschonec/b1fa89420e601d565b058b5dc4010517 to your computer and use it in GitHub Desktop.
---
- name: Take or remove a Snapshot of a VMWare guest.
gather_facts: false
become: false
hosts: all
serial:
- 1
- 100%
max_fail_percentage: 1
vars_prompt:
- name: my_password
prompt: "Password for VMWare: "
private: true
tasks:
- name: Put a block around all of this stuff because it's all delegate_to localhost.
block:
- name: Do a minimal setup so that we can get some standard Ansible variables.
setup:
gather_subset:
- '!all'
- 'min'
- name: Display all of the datacenters.
ansible.builtin.debug:
msg: "The datacenters are http://{{ item.0.url }} at {{ item.1 }}."
with_subelements:
- "{{ vmware }}"
- datacenters
run_once: true
- name: Query VCenter for the host names
command: "./govc_linux_amd64 vm.info -u {{ item.0.url }} -dc='{{ item.1 }}' -json=true {{ inventory_hostname_short }}"
args:
chdir: "{{ playbook_dir }}"
with_subelements:
- "{{ vmware }}"
- datacenters
register: govc
changed_when: false
environment:
GOVC_USERNAME: "{{ my_username | default(ansible_user_id) }}"
GOVC_PASSWORD: "{{ my_password }}"
GOVC_INSECURE: 1
no_log: true
# The vm_find_query fact should contain all the dacenters in all the 'clusters' even if the VM wasn't found.
- name: Convert the GOVC array of JSON formatted stdout to an array of real variables/values.
set_fact:
vm_find_query: "{{ vm_find_query | default([]) + ([item.stdout | from_json]) }}"
loop: "{{ govc.results }}"
no_log: true
- name: Inject hackishly the URL and datacenter into the vm_find_query fact
set_fact:
vms_with_datacenters: "{{ vms_with_datacenters | default([]) + [{'datacenter':item.1, 'url':item.0.url, 'VirtualMachines': vm_find_query[index].VirtualMachines}] }}"
with_subelements:
- "{{ vmware }}"
- datacenters
loop_control:
index_var: index
no_log: true
- name: Build a list of the results from the GOVC query.
debug:
msg: "{{ inventory_hostname_short }} found at {{ item.url }} on datacenter: {{ item.datacenter }} with UUID of: {{ item.VirtualMachines[0].Config.Uuid }}"
loop: "{{ vms_with_datacenters }}"
when:
- "item.VirtualMachines | type_debug != 'NoneType'"
- false
no_log: true
# VirtualMachines: None indicates "not found". Do not add those "not found" to the my_fact fact.
# The VirtualMachines key is what govc returns from the command line. If a VMis found, you'll
# get a WHOLE BUNCH of stuff returned. If the VM is not found, you'll get an empty set.
# We don't add to the found_vms fact because the found_vms fact is specific to the individual
# servers we're snapshotting and we want only *one* entry for the found_vms fact per server.
- name: Print out the servers found and their UUIDs.
ansible.builtin.debug:
msg: "Server {{ inventory_hostname_short }} found at {{ found_vms[0].Config.Uuid }}."
when: false
- name: "Take a snapshot of the server."
vmware_guest_snapshot:
hostname: "{{ item.url }}"
validate_certs: false
username: "{{ my_username | default(ansible_user_id) }}"
password: "{{ my_password }}"
uuid: "{{ item.VirtualMachines[0].Config.Uuid | lower }}"
datacenter: "{{ item.datacenter }}"
snapshot_name: "Snapshot before patching."
state: "{{ snapshot_state | default('present') }}"
loop: "{{ vms_with_datacenters }}"
when: "item.VirtualMachines | type_debug != 'NoneType'"
no_log: true
delegate_to: localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment