-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvm-restore-snapshots.yml
More file actions
82 lines (69 loc) · 2.76 KB
/
vm-restore-snapshots.yml
File metadata and controls
82 lines (69 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# execution:
# $ ansible-playbook vm-restore-snapshots.yml -e 'vmVars=iad.lab/mini-cns-vars.yml' -e 'snapDescription="Before *"'
---
- hosts: localhost
gather_facts: yes
vars_files:
- "{{ ansible_domain }}/rhvm-vault.yml"
tasks:
- assert:
that: snapDescription is defined
msg: You must provide the common snapshot description with -e 'snapDescription="something*"'
- name: Load VM vars
include_vars: "{{ vmVars }}"
- name: Login to RHV
ovirt_auth:
url: "{{ rhvurl }}"
insecure: yes
username: "{{ rhvuser }}"
password: "{{ rhvpass }}"
- name: Query snapshot IDs
ovirt_snapshot_facts:
auth: "{{ ovirt_auth }}"
vm: "{{ item.key }}"
description: "{{ snapDescription }}"
with_dict: "{{ vms }}" # See home.lab/ocp-cns-vars.yml
register: myBigFatBlob
#TODO: Add some logic here to check for returned results, and fail if no results found
# - debug: var=myBigFatBlob
# - debug: var=myBigFatBlob.results
# - debug: var=myBigFatBlob.results[0].ansible_facts.ovirt_snapshots[0].vm.name
# - debug: var=myBigFatBlob.results[0].ansible_facts.ovirt_snapshots[0].description
# - debug: var=myBigFatBlob.results[0].ansible_facts.ovirt_snapshots[0].id
# https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results
- name: Create a new array from the query results
set_fact:
mySnaps: "{{ mySnaps|default({}) | combine( {item.ansible_facts.ovirt_snapshots[0].vm.name: item.ansible_facts.ovirt_snapshots[0].id} ) }}"
with_items: "{{ myBigFatBlob.results }}"
no_log: true
- debug: var=mySnaps
# https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results
- name: Create a list of dicts instead as another new variable
set_fact:
mySnaps2: "{{ mySnaps2|default([]) + [ {'name': item.ansible_facts.ovirt_snapshots[0].vm.name, 'id': item.ansible_facts.ovirt_snapshots[0].id} ] }}"
with_items: "{{ myBigFatBlob.results }}"
no_log: true
- debug: var=mySnaps2
- name: Make sure the VMs are stopped
ovirt_vm:
auth: "{{ ovirt_auth }}"
name: "{{ item.name }}"
state: stopped
loop: "{{ mySnaps2}}"
- name: Restore to the chosen snapshot
ovirt_snapshot:
auth: "{{ ovirt_auth }}"
state: restore
vm_name: "{{ item.name }}"
snapshot_id: "{{ item.id }}"
loop: "{{ mySnaps2}}"
- name: Startup the VMs
ovirt_vm:
auth: "{{ ovirt_auth }}"
name: "{{ item.name }}"
state: running
loop: "{{ mySnaps2}}"
- name: Cleanup RHV auth token
ovirt_auth:
ovirt_auth: "{{ ovirt_auth }}"
state: absent