-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcreate_RHV_VM_from_template.yml
131 lines (107 loc) · 3.36 KB
/
create_RHV_VM_from_template.yml
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
---
- hosts: localhost
# gather_facts: false
connection: local
vars:
max_retries: 9
retry_interval: 20
tasks:
- name: Set the Provider URL
set_fact:
provider_url: "{{ manageiq.api_url }}/api/providers/{{ provider_id }}"
- debug: var=provider_url
- name: Set the Service URL
set_fact:
svc_url: "/api/{{ manageiq.service }}"
- debug: var=svc_url
- name: Obtain SSO token
ovirt_auth:
url: "{{ ovirt_url }}"
username: "{{ ansible_env.OVIRT_USERNAME }}"
password: "{{ ansible_env.OVIRT_PASSWORD }}"
insecure: true
- name: Create VM
ovirt_vm:
auth: "{{ ovirt_auth }}"
state: present
name: "{{ vm_name }}"
template: "{{ vm_template }}"
cluster: "{{ cluster }}"
register: new_vm
- name: Revoke SSO token
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
- debug: var=new_vm
- set_fact: new_vm_uuid="{{ new_vm.id }}"
- debug: var=new_vm_uuid
- name: Refresh provider in CloudForms
uri:
url: "{{ provider_url }}"
method: POST
body:
action: refresh
body_format: json
validate_certs: False
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- debug: var=output
- name: set the task href
set_fact:
task_url: "{{ output.json.task_href}}"
- debug: var=task_url
- name: Wait for the refresh to end
uri:
url: "{{ task_url }}"
method: GET
validate_certs: False
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: task_result
until: task_result.json.state == 'Finished' or task_result.json.status == 'Error'
failed_when: task_result.json.status == 'Error'
retries: "{{max_retries}}"
delay: "{{retry_interval}}"
- debug: var=task_result
- set_fact: find_vm_url="{{ manageiq.api_url }}/api/vms?expand=resources&attributes=href,uid_ems&filter[]=uid_ems='{{ new_vm_uuid }}'"
- debug: var=find_vm_url
- name: Call to CloudForms to find href for new_vm_uuid
uri:
url: "{{ find_vm_url }}"
method: GET
validate_certs: no
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
body_format: json
return_content: yes
register: token_output
- debug: var=token_output
- set_fact: cf_vm_href="{{ token_output.json.resources[0].href }}"
- debug: var=cf_vm_href
- name: Create an array of vms
set_fact:
vms: "{{ vms|default([]) + [ { 'href': svc_url, 'resource': { 'href': item } } ] }}"
with_items:
- "{{ cf_vm_href }}"
- debug: var=vms
- name: Register vm with the service
uri:
url: "{{ manageiq.api_url }}/api/services"
method: POST
body_format: json
body:
action: add_resource
resources : "{{ vms }}"
validate_certs: False
headers:
X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- debug: var=output.json.results[0].success