-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathESXi7.0.u2_provisioning.yml
517 lines (443 loc) · 20.2 KB
/
ESXi7.0.u2_provisioning.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
---
# Performing automated ESXi 7.0 U2 Boot from SAN installation on Synergy Module(s) using a kickstart and a HPE OneView Server Profile Template
#
# Command that can be used to run this playbook:
# $ ansible-playbook -i hosts ESXi7.0.u2_provisioning.yml --ask-vault-password --ask-become-pass
#>
# Adding a DNS record for the server that will be provisioned in the defined DNS server
- name: Creating a DNS record for the bare metal ESXi server
hosts: ESX
gather_facts: no
vars:
ansible_forks: 5
vars_files:
- vars/Windows_DNS_vars_encrypted.yml
tasks:
- name: Adding "{{ inventory_hostname }}" with "{{ os_ip_address }}" on "{{ dns_server }}" in "{{ domain }}" DNS domain
community.windows.win_dns_record:
name: "{{ inventory_hostname }}"
type: "A"
value: "{{ os_ip_address }}"
zone: "{{ domain }}"
state: present
delegate_to: "{{ dns_server }}"
- name: Performing an automated ESXi 7.0 U2 Boot from SAN installation on a Synergy Module using a kickstart and a OneView Server Profile Template
hosts: ESX
collections:
- hpe.oneview
gather_facts: no
vars_files:
- vars/ESXi7.0.u2_vars.yml
- vars/VMware_vCenter_vars_encrypted.yml
- vars/iLO_vars_encrypted.yml # Define the iLO local account created by the server profile template. This account is required by the community.general.hpilo_boot module to manage the server iLO
vars:
# HPE Synergy Composer configuration
config: "{{ playbook_dir }}/oneview_config.json"
inventory_fqdn: "{{ inventory_hostname | lower }}.{{ domain }}"
# ansible_python_interpreter: /usr/bin/python3
ansible_host_key_checking: false
validate_certs: false
ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}"
ansible_ssh_public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
ansible_forks: 5
tasks:
- name: Checking if HPE ESXi Custom ISO file "{{ src_iso_file }}" exists in "{{ src_iso_directory }}" on "{{lookup("pipe","hostname")}}"
stat:
path: "{{ src_iso_directory }}/{{ src_iso_file }}"
register: ISO_Present
delegate_to: localhost
- name: Creating the directory "{{ src_iso_directory }}" to host the ISO file on "{{ lookup("pipe","hostname") }}"
become: yes
file:
path: "{{ src_iso_directory }}"
state: directory
when: ISO_Present.stat.exists == False
delegate_to: localhost
- name: Downloading file "{{ src_iso_file }}" to "{{ lookup("pipe","hostname") }}" in "{{ src_iso_directory }}" if not present
become: yes
get_url:
url: "{{ src_iso_url }}/{{ src_iso_file }}"
dest: "{{ src_iso_directory }}"
validate_certs: no
when: ISO_Present.stat.exists == False
delegate_to: localhost
- name: Checking if HPE ESXi Custom ISO file extraction is necessary in "{{ staging_directory }}/baremetal/{{ inventory_hostname }}" on "{{ lookup("pipe","hostname") }}"
stat:
path: "{{ staging_directory }}/baremetal/{{ inventory_hostname }}"
register: ISO_Extracted
delegate_to: localhost
# - debug: var=ISO_Extracted
- name: Creating "/mnt/{{ inventory_hostname }}" on "{{lookup("pipe","hostname")}}" if it does not exist
become: yes
ansible.builtin.file:
path: /mnt/{{ inventory_hostname }}
state: directory
mode: "0755"
delegate_to: localhost
- name: Creating "{{ staging_directory }}/baremetal/{{ inventory_hostname }}/" on "{{lookup("pipe","hostname")}}" if it does not exist
become: yes
ansible.builtin.file:
path: "{{ staging_directory }}/baremetal/{{ inventory_hostname }}"
state: directory
mode: "0755"
delegate_to: localhost
- name: Mounting HPE ESXi Custom ISO "{{ src_iso_directory }}/{{ src_iso_file }}" to "/mnt/{{ inventory_hostname }}/" and copying ISO files to "{{ staging_directory }}/baremetal/{{ inventory_hostname }}/" on "{{lookup("pipe","hostname")}}"
become: yes
shell: |
mount -o loop -t iso9660 --read-only {{ src_iso_directory }}/{{ src_iso_file }} /mnt/{{ inventory_hostname }}/
cp -r /mnt/{{ inventory_hostname }}/. {{ staging_directory }}/baremetal/{{ inventory_hostname }}/
umount /mnt/{{ inventory_hostname }}
# when: ISO_Extracted.stat.exists == False
delegate_to: localhost
- name: Modifying UEFI bootloader for kickstart installation from CDROM
become: yes
shell: |
sed -i 's/kernelopt=cdromBoot runweasel/kernelopt=runweasel ks=cdrom:\/KS.CFG/' {{ staging_directory }}/baremetal/{{ inventory_hostname }}/efi/boot/boot.cfg
delegate_to: localhost
# Creating a Server Profile in HPE OneView from a boot from SAN Server Profile Template:
- name: Creating Server Profile "{{ inventory_hostname }}" from Server Profile Template "{{ server_template }}"
oneview_server_profile:
config: "{{ config }}"
data:
serverProfileTemplateName: "{{ server_template }}"
name:
"{{ inventory_hostname }}"
# serverHardwareUri: "/rest/server-hardware/39313738-3234-584D-5138-323830343848"
# server_hardware: Encl1, bay 12
# If any hardware is provided, it tries to get one available
delegate_to: localhost
register: result
# - debug: var=server_profile
# - debug: var=serial_number
# - debug: var=server_hardware
# - debug: var=compliance_preview
# - debug: var=created
- name: Capturing the boot information of the first fiber channel interface of the server profile
set_fact:
fc_bootable: "{{ (server_profile.connectionSettings.connections | selectattr('functionType', 'equalto', 'FibreChannel') | map(attribute='boot.priority') | list)[0] }}"
# - debug: var=fc_bootable
- name: Capturing network set information from "{{ VM_network_set }}" attached to the two production NICs
oneview_network_set_facts:
config: "{{ config }}"
name: "{{ VM_network_set }}"
delegate_to: localhost
# - debug: var=network_sets
- name: Capturing the URI of network "{{ VM_network_set }}" attached to the two production NICs
set_fact:
VM_network_set_uri: "{{ network_sets[0].uri }}"
# - debug: var=VM_network_set_uri
- name: Capturing the server hardware name selected for Server Profile creation
set_fact:
server_hardware_name: "{{ server_hardware.name }}"
- name: Capturing MAC addresses of the production NICs attached to "{{ VM_network_set }}" for subsequent configuration of the Distributed vSwitch.
set_fact:
mac_prod_1: "{{ (server_profile.connectionSettings.connections | selectattr('networkUri', 'equalto', VM_network_set_uri) | map(attribute='mac') | list)[0] }}"
mac_prod_2: "{{ (server_profile.connectionSettings.connections | selectattr('networkUri', 'equalto', VM_network_set_uri) | map(attribute='mac') | list)[1] }}"
# - debug: var=mac_prod_1
# - debug: var=mac_prod_2
- name: Capturing LUN uri of the primary boot volume (if any) for the customization of the kickstart file
set_fact:
lunuri: "{{ (server_profile.sanStorage.volumeAttachments | selectattr('bootVolumePriority', 'equalto', 'Primary') | map(attribute='volumeUri') | list)[0] }}"
when: fc_bootable == "Primary" or fc_bootable == "Secondary"
# - debug: var=lunuri
- name: Showing the result of the Server Profile creation task
debug:
msg: "Hardware selected: {{ server_hardware_name }} - Result: {{ result.msg }}"
- name: Capturing boot volume information (if any)
oneview_volume_facts:
config: "{{ config }}"
delegate_to: localhost
when: fc_bootable == "Primary" or fc_bootable == "Secondary"
# - debug: var=storage_volumes
- name: Capturing boot LUN size defined in the Server Profile to ensure that OS will be installed on this disk using the kickstart file
set_fact:
boot_lun_size: "{{ ((storage_volumes | selectattr('uri', 'equalto', lunuri) | map(attribute='provisionedCapacity') | list)[0] | int / (1024*1024*1024) ) |int}}"
when: fc_bootable == "Primary" or fc_bootable == "Secondary"
- name: Setting boot LUN size as 'undefined' if booting from local logical drive
set_fact:
boot_lun_size: "undefined"
when: fc_bootable == "NotBootable"
# - debug: var=boot_lun_size
# Creation of the ks.cfg kickstart file
- name: Creating kickstart file with %pre script to detect the "{{ boot_lun_size }}GB" Boot From SAN volume if it exists
become: yes
template:
src: files/{{ esxi_build }}/{{ kickstart }}
dest: "{{ staging_directory }}/baremetal/{{ inventory_hostname }}/KS.CFG"
delegate_to: localhost
- name: Prepare KS.CFG kickstart file to make the new ISO in '{{ staging_directory }}/baremetal/{{ inventory_hostname }}/KS.CFG'
become: yes
file:
path: "{{ staging_directory }}/baremetal/{{ inventory_hostname }}/KS.CFG"
state: file
mode: "0755"
delegate_to: localhost
# Creation of the new ESXi ISO image with unattended installation
- name: Creating customized bootable ISO in "{{ staging_directory }}/baremetal/{{ inventory_hostname }}/"
become: yes
shell: >
mkisofs
-relaxed-filenames
-J
-R
-b isolinux.bin -iso-level 2
-c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table
-eltorito-alt-boot -eltorito-boot efiboot.img -no-emul-boot
-o {{ staging_directory }}/baremetal/{{ inventory_hostname }}.iso
{{ staging_directory }}/baremetal/{{ inventory_hostname }}/
delegate_to: localhost
- name: Creating "/usr/share/nginx/html/isos/" on "{{lookup("pipe","hostname")}}" if it does not exist
become: yes
file:
path: /usr/share/nginx/html/isos/
state: directory
mode: "0755"
delegate_to: localhost
- name: Moving created ISO to the nginx default html folder "http://{{ lookup("pipe","hostname") }}/isos"
become: yes
shell: |
mv {{ staging_directory }}/baremetal/{{ inventory_hostname }}.iso /usr/share/nginx/html/isos/
delegate_to: localhost
- name: Update SELinux security contexts so that Nginx is allowed to serve content from the "/usr/share/nginx/html/isos/" directory.
become: yes
shell: |
chcon -vR system_u:object_r:httpd_sys_content_t:s0 /usr/share/nginx/html/isos/
delegate_to: localhost
# Starting the OS unattended installation
- name: Powering on and booting "{{ server_hardware_name }}" from created ISO using iLO Virtual Media
community.general.hpilo_boot:
host: "{{ server_hardware.mpHostInfo.mpIpAddresses[1].address }}"
login: "{{ iLO_username }}"
password: "{{ iLO_password }}"
media: cdrom
image: 'http://{{ lookup("pipe","hostname") }}/isos/{{ inventory_hostname }}.iso'
delegate_to: localhost
- name: Waiting for ESXi installation to complete - waiting for "{{ os_ip_address }}" to respond...
wait_for:
timeout: 2000
host: "{{ os_ip_address }}"
port: 80
delegate_to: localhost
register: boot_wait_time
- debug:
msg: "{{ inventory_hostname }} installation took {{ (boot_wait_time.elapsed / 60) | round | int }} minutes"
- name: Wait a little longer so that the ESX host is truly ready to be added to vCenter
wait_for:
timeout: 60
delegate_to: localhost
# Backup kickstart file for troubleshooting purposes
- name: Create a kickstart backup directory if it does not exist
become: yes
file:
path: "{{ staging_directory }}/kickstarts_backup/ESXi"
state: directory
delegate_to: localhost
- name: Create a backup of the kickstart file named '{{ inventory_hostname }}_ks.cfg' in '{{ staging_directory }}/kickstarts_backup/ESXi' folder
become: yes
shell: |
cp -f {{ staging_directory }}/baremetal/{{ inventory_hostname }}/KS.CFG {{ staging_directory }}/kickstarts_backup/ESXi/{{ inventory_hostname }}_ks.cfg
delegate_to: localhost
# Cleaning up staging files
- name: Deleting all related files from staging location and nginx web server folder
become: yes
shell: |
rm -rf {{ inventory_hostname }}
rm -f /usr/share/nginx/html/isos/{{ inventory_hostname }}.iso
rm -rf /mnt/{{ inventory_hostname }}
args:
chdir: "{{ staging_directory }}/baremetal"
delegate_to: localhost
# vCenter and ESXi configuration
- name: Creating ESXi cluster "{{ cluster_name }}" in vCenter "{{ vcenter_hostname }}" if not present
community.vmware.vmware_cluster:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: "{{ datacenter_name }}"
cluster_name: "{{ cluster_name }}"
validate_certs: false
delegate_to: localhost
- name: Enabling HA without admission control on "{{ cluster_name }}" cluster
community.vmware.vmware_cluster_ha:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: "{{ datacenter_name }}"
cluster_name: "{{ cluster_name }}"
enable: true
validate_certs: false
delegate_to: localhost
- name: Enabling DRS with VM distribution across hosts for availability on "{{ cluster_name }}" cluster
community.vmware.vmware_cluster_drs:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: "{{ datacenter_name }}"
cluster_name: "{{ cluster_name }}"
enable: true
advanced_settings:
'TryBalanceVmsPerHost': '1'
validate_certs: false
delegate_to: localhost
- name: Adding ESXi host "{{ inventory_fqdn }}" to vCenter "{{ vcenter_hostname }}"
vmware_host:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: "{{ datacenter_name }}"
cluster_name: "{{ cluster_name }}"
esxi_hostname: "{{ inventory_fqdn }}"
esxi_username: "root"
esxi_password: "{{ root_password }}"
state: present
validate_certs: false
delegate_to: localhost
- name: Assigning ESXi license to Host
vcenter_license:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
license: "{{ esxi_license }}"
state: present
validate_certs: false
delegate_to: localhost
- name: Adding vmnic1 to standard switch vSwitch0
vmware_vswitch:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
switch: vSwitch0
nics:
- vmnic0
- vmnic1
validate_certs: false
delegate_to: localhost
- name: Pause for 10 seconds to give time for the vswitch0 configuration
ansible.builtin.pause:
seconds: 10
- name: Adding vMotion Portgroup to standard switch vSwitch0
vmware_portgroup:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
switch: "vSwitch0"
portgroup: "vMotion"
validate_certs: false
delegate_to: localhost
- name: Pause for 10 seconds to give time for the vswitch0 configuration
ansible.builtin.pause:
seconds: 10
- name: Gathering facts about vmnics
community.vmware.vmware_host_vmnic_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
validate_certs: false
delegate_to: localhost
register: host_vmnics
# - debug: var=host_vmnics
- name: Capturing Production vmnics information for the distributed switch creation
set_fact:
vmnic_prod_1: '{{ (host_vmnics.hosts_vmnics_info | json_query(''"'' + inventory_fqdn + ''".vmnic_details'') | selectattr(''mac'', ''equalto'', mac_prod_1 | lower ) | map(attribute=''device'') | list )[0] }}'
vmnic_prod_2: '{{ (host_vmnics.hosts_vmnics_info | json_query(''"'' + inventory_fqdn + ''".vmnic_details'') | selectattr(''mac'', ''equalto'', mac_prod_2 | lower ) | map(attribute=''device'') | list )[0] }}'
# - debug: var=vmnic_prod_1
# - debug: var=vmnic_prod_2
- name: Connecting host to "{{ vcenter_switch_name }}" distributed switch
vmware_dvs_host:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
switch_name: "{{ vcenter_switch_name }}"
vmnics:
- "{{ vmnic_prod_1 }}"
- "{{ vmnic_prod_2 }}"
state: present
validate_certs: False
delegate_to: localhost
- name: Adding vmkernel mk1 port to "{{ vcenter_switch_name }}" distributed Switch
community.vmware.vmware_vmkernel:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
dvswitch_name: "{{ vcenter_switch_name }}"
portgroup_name: "{{ dportgroup_name }}"
network:
type: "dhcp"
state: present
device: vmk1
enable_provisioning: True
validate_certs: False
delegate_to: localhost
- name: Changing Advanced Settings with Core Dump Warning Disable
vmware_host_config_manager:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
options:
"UserVars.SuppressCoredumpWarning": "1"
validate_certs: false
delegate_to: localhost
- name: Setting the Power Management Policy to high-performance
vmware_host_powermgmt_policy:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
policy: high-performance
validate_certs: false
delegate_to: localhost
- name: Configuring NTP servers
vmware_host_ntp:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
ntp_servers:
- pool.ntp.org
validate_certs: false
delegate_to: localhost
- name: Starting NTP Service and set to start at boot.
vmware_host_service_manager:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
validate_certs: false
service_name: ntpd
service_policy: on
state: start
delegate_to: localhost
- name: Starting ESXi Shell Service and setting to enable at boot . . .
vmware_host_service_manager:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
validate_certs: false
service_name: TSM
service_policy: on
state: start
delegate_to: localhost
- name: Starting SSH Service and setting to enable at boot.
vmware_host_service_manager:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
validate_certs: false
service_name: TSM-SSH
service_policy: on
state: start
delegate_to: localhost
- name: Displaying install completed message
debug:
msg:
- "{{ inventory_hostname }}.{{domain}} Installation completed !"
- "ESXi is configured and running. It has been added to the vCenter cluster '{{ cluster_name }}'."