-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcenter_vli_deploy.py
361 lines (304 loc) · 11.6 KB
/
vcenter_vli_deploy.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python
#
# (c) 2015, Joseph Callen <jcallen () csc.com>
# Portions Copyright (c) 2015 VMware, Inc. All rights reserved.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
module: vcenter_vli_deploy
Short_description: Deploys (creates), Deletes log insight ova to vcenter cluster
description:
Deploys (creates), Deletes log insight ova to vcenter cluster. Module will wait for vm to
power on and "pings" the log insight api before exiting if not failed.
requirements:
- pyvmomi 6
- ansible 2.x
Tested on:
- vcenter 6.0
- pyvmomi 6
- esx 6
- ansible 2.1.2
- VMware-vRealize-Log-Insight-3.0.1-3568951.ova
options:
hostname:
description:
- The hostname or IP address of the vSphere vCenter API server
required: True
username:
description:
- The username of the vSphere vCenter with Admin rights
required: True
aliases: ['user', 'admin']
password:
description:
- The password of the vSphere vCenter user
required: True
aliases: ['pass', 'pwd']
datacenter:
description:
- The name of the datacenter.
required: True
cluster:
description:
- The name of the vCenter cluster
required: True
vmname:
description:
- The name of the vm in vcenter
required: True
ovftool_path:
description:
- The path where the ovftool is installed
ex: /usr/local/bin/ovftool
path_to_ova:
description:
- The path where the ova is located
required: True
ova_file:
description:
- The name of the ova file
required: True
disk_mode:
description:
- The disk mode for the deployment of the ova
default: thin
required: True
datastore:
description:
- Valid vcenter datastore
required: True
network:
description:
- Name of the network/portgroup for the appliance
required: True
gateway:
description:
- gatway information for the appliance
required: True
dns_ip:
description:
- dns server ip address
type: list
ip_addr:
description:
- ip address for the appliance
required: True
netmask:
description:
- netmask information for the appliance
required: True
root_password:
description:
- root password for the appliance
required: True
deployment_size:
description:
- size of the deployment for the appliance
required: True
vli_hostname:
description:
- hostname for the appliance
required: True
state:
description:
- Desired state of the disk group
choices: ['present', 'absent']
required: True
'''
EXAMPLE = '''
- name: deploy vR Log Insight Appliance
vcenter_vli_deploy:
hostname: "{{ vcenter }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
vmname: "{{ vli_vmname }}"
ovftool_path: "{{ ovf_tool_path }}"
path_to_ova: "{{ ova_path }}"
ova_file: "{{ vrli_ova }}"
datacenter: "{{ datacenter.name }}"
cluster: "{{ ib_vcenter_mgmt_esx_cluster_name }}"
disk_mode: "{{ disk_mode }}"
datastore: "{{ ib_vcenter_mgmt_esx_cluster_name }}_VSAN_DS"
vli_hostname: "{{ vrli_hostname }}"
network: "{{ mgmt_vds_viomgmt }}"
gateway: "{{ vrli_gateway }}"
dns_ip: "{{ ova_dns_list }}"
ip_addr: "{{ vrli_ip_addr }}"
netmask: "{{ vrli_netmask }}"
root_password: "{{ vrli_rootpw }}"
deployment_size: "{{ vli_deployment_size }}"
state: "{{ global_state }}"
tags:
- vio_deploy_vrli_ova
'''
try:
import time
import requests
from pyVmomi import vim, vmodl
IMPORTS = True
except ImportError:
IMPORTS = False
vc = {}
def check_vli_api(module):
url = "https://{}/api/v1".format(module.params['ip_addr'])
auth = requests.auth.HTTPBasicAuth('root', module.params['root_password'])
header = {'Content-Type': 'application/json'}
try:
resp = requests.get(url=url, verify=False,
auth=auth, headers=header)
except requests.exceptions.ConnectionError:
return False
return resp.status_code, resp.content
def wait_for_api(module, sleep_time=15):
status_poll_count = 0
while status_poll_count < 30:
api_status = check_vli_api(module)
if api_status:
if api_status[0] == 200:
return True
else:
status_poll_count += 1
time.sleep(sleep_time)
else:
status_poll_count += 1
time.sleep(sleep_time)
if status_poll_count == 30:
return False
def wait_for_vm(vm, sleep_time=15):
vm_pool_count = 0
while vm_pool_count < 30:
connected = (vm.runtime.connectionState == 'connected')
if connected:
powered_on = (vm.runtime.powerState == 'poweredOn')
if powered_on:
return True
else:
vm_pool_count += 1
time.sleep(sleep_time)
else:
vm_pool_count += 1
time.sleep(sleep_time)
if vm_pool_count == 30:
return False
def find_virtual_machine(content, searched_vm_name):
virtual_machines = get_all_objs(content, [vim.VirtualMachine])
for vm in virtual_machines:
if vm.name == searched_vm_name:
return vm
return None
def state_delete_vm(module):
changed = False
vm = vc['vli_vm']
if vm.runtime.powerState == 'poweredOn':
power_off_task = vm.PowerOffVM_Task()
wait_for_task(power_off_task)
try:
delete_vm_task = vm.Destroy_Task()
changed, result = wait_for_task(delete_vm_task)
except Exception as e:
module.fail_json(msg="Failed deleting vm: {}".format(str(e)))
module.exit_json(changed=changed)
def state_exit_unchanged(module):
module.exit_json(changed=False, msg="EXIT UNCHANED")
def state_create_vm(module):
ovftool_exec = '{}/ovftool'.format(module.params['ovftool_path'])
ova_file = '{}/{}'.format(module.params['path_to_ova'], module.params['ova_file'])
vi_string = 'vi://{}:{}@{}/{}/host/{}/'.format(module.params['username'],
module.params['password'], module.params['hostname'],
module.params['datacenter'], module.params['cluster'])
ova_tool_result = module.run_command([ovftool_exec,
'--acceptAllEulas',
'--skipManifestCheck',
'--overwrite',
'--powerOn',
'--noSSLVerify',
'--allowExtraConfig',
'--name={}'.format(module.params['vmname']),
'--prop:vm.rootpw={}'.format(module.params['root_password']),
'--diskMode={}'.format(module.params['disk_mode']),
'--datastore={}'.format(module.params['datastore']),
'--net:Network 1={}'.format(module.params['network']),
'--prop:vami.ip0.VMware_vCenter_Log_Insight={}'.format(module.params['ip_addr']),
'--prop:vami.gateway.VMware_vCenter_Log_Insight={}'.format(module.params['gateway']),
'--prop:vami.DNS.VMware_vCenter_Log_Insight={},{}'.format(module.params['dns_ip'][0],
module.params['dns_ip'][1]),
'--prop:vami.netmask0.VMware_vCenter_Log_Insight={}'.format(module.params['netmask']),
'--prop:vami.hostname.VMware_vCenter_Log_Insight={}'.format(module.params['vli_hostname']),
'--deploymentOption={}'.format(module.params['deployment_size']),
ova_file,
vi_string])
if ova_tool_result[0] != 0:
module.fail_json(msg='Failed to deploy OVA, error message from ovftool is: {}'.format(ova_tool_result[1]))
return ova_tool_result[0]
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
dict(
vmname=dict(required=True, type='str'),
ovftool_path=dict(required=True, type='str'),
path_to_ova=dict(required=True, type='str'),
ova_file=dict(required=True, type='str'),
datacenter=dict(required=True, type='str'),
cluster=dict(required=True, type='str'),
disk_mode=dict(default='thin', type='str'),
datastore=dict(required=True, type='str'),
network=dict(required=True, type='str'),
vli_hostname=dict(required=True, type='str'),
gateway=dict(required=True, type='str'),
dns_ip=dict(required=True, type='list'),
ip_addr=dict(required=True, type='str'),
netmask=dict(required=True, type='str'),
ip_protocol=dict(type='str', default="IPv4"),
deployment_size=dict(required=True, type='str'),
root_password=dict(required=True, type='str', no_log=True),
state=dict(default='present', choices=['present', 'absent']),
)
)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
if not IMPORTS:
module.fail_json(msg="Failed to import modules")
content = connect_to_api(module)
vli_vm = find_virtual_machine(content, module.params['vmname'])
vc['vli_vm'] = vli_vm
vli_vm_states = {
'absent': {
'present': state_delete_vm,
'absent': state_exit_unchanged,
},
'present': {
'present': state_exit_unchanged,
'absent': state_create_vm
}
}
desired_state = module.params['state']
if vli_vm:
current_state = 'present'
else:
current_state = 'absent'
vli_vm_states[desired_state][current_state](module)
vli_vm = find_virtual_machine(content, module.params['vmname'])
if not vli_vm:
module.fail_json(changed=False, msg="Failed to find vm")
if not wait_for_vm(vli_vm):
module.fail_json(msg="VM failed to power on")
if not wait_for_api(module):
module.fail_json(msg="Failed to hit api")
module.exit_json(changed=True, result="Success")
from ansible.module_utils.basic import *
from ansible.module_utils.vmware import *
if __name__ == '__main__':
main()