-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDeployUbuntu.yml
More file actions
250 lines (213 loc) · 9.24 KB
/
Copy pathDeployUbuntu.yml
File metadata and controls
250 lines (213 loc) · 9.24 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
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
---
- hosts: localhost
name: DeployUbuntu.yml
vars_files:
- variables.yml
gather_facts: false
tasks:
- name: Create working directory on Ansible Controller
ansible.builtin.file:
path: "{{ Global.Workingdir }}"
state: directory
mode: "755"
- name: Check if Ubuntu ISO exists locally on Ansible Controller
ansible.builtin.stat:
path: "{{ Global.Workingdir }}/{{ Global.UbuntuISO }}"
register: installerfilecheck
- name: Download Ubuntu ISO if ISO file doesn't exist locally
ansible.builtin.get_url:
url: "{{ Global.UbuntuISO_URL }}{{ Global.UbuntuISO }}"
dest: "{{ Global.Workingdir }}/{{ Global.UbuntuISO }}"
mode: "755"
when:
- not installerfilecheck.stat.exists
- name: Extract Ubuntu ISO
ansible.builtin.command: "7z -y x {{ Global.Workingdir }}/{{ Global.UbuntuISO }} \
-o{{ Global.Workingdir }}/ISO"
changed_when: false
- name: Move [BOOT] out of ISO extraction directory
ansible.builtin.command: "mv {{ Global.Workingdir }}/ISO/'[BOOT]' {{ Global.Workingdir }}/BOOT"
changed_when: false
- name: Create directory to store user-data and meta-data
ansible.builtin.file:
path: "{{ Global.Workingdir }}/ISOcopy/{{ item.Name }}/autoinstall"
state: directory
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Copy user-data file(s) to directory
ansible.builtin.template:
src: ./Ubuntu_user-data.j2
dest: "{{ Global.Workingdir }}/ISOcopy/{{ item.Name }}/autoinstall/user-data"
mode: "666"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Copy grub.cfg file into ISO extraction directory
ansible.builtin.copy:
src: ./Ubuntu_grub.cfg
dest: "{{ Global.Workingdir }}/ISO/boot/grub/grub.cfg"
mode: "666"
- name: Create empty meta-data file(s)
ansible.builtin.file:
path: "{{ Global.Workingdir }}/ISOcopy/{{ item.Name }}/autoinstall/meta-data"
state: touch
mode: "666"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Create custom Ubuntu ISO(s)
ansible.builtin.command: "xorriso -as mkisofs -r \
-V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)' \
-o {{ Global.Workingdir }}/{{ item.Name }}.iso \
--grub2-mbr {{ Global.Workingdir }}/BOOT/1-Boot-NoEmul.img \
-partition_offset 16 \
--mbr-force-bootable \
-append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b {{ Global.Workingdir }}/BOOT/2-Boot-NoEmul.img \
-appended_part_as_gpt \
-iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 \
-c '/boot.catalog' \
-b '/boot/grub/i386-pc/eltorito.img' \
-no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
-eltorito-alt-boot \
-e '--interval:appended_partition_2:::' \
-no-emul-boot \
{{ Global.Workingdir }}/ISO/ {{ Global.Workingdir }}/ISOcopy/{{ item.Name }}"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Upload the custom Ubuntu ISO(s) to the vSphere datastore
community.vmware.vsphere_copy:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
datacenter: "{{ Global.ISODatacenter }}"
src: "{{ Global.Workingdir }}/{{ item.Name }}.iso"
datastore: "{{ Global.ISODatastore }}"
path: "{{ Global.ISODatastoreDir }}/{{ item.Name }}.iso"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Deploy Ubuntu VM(s)
community.vmware.vmware_guest:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
name: "{{ item.Name }}"
state: "{{ item.Configuration.VM.PowerState }}"
guest_id: "{{ item.Configuration.VM.GuestID }}"
datacenter: "{{ item.Placement.Datacenter }}"
cluster: "{{ item.Placement.Cluster }}"
folder: "{{ item.Placement.Folder }}"
disk:
- size_gb: "{{ item.Configuration.Storage.DiskSize }}"
type: "{{ item.Configuration.Storage.DiskType }}"
datastore: "{{ item.Placement.Datastore }}"
hardware:
memory_mb: "{{ item.Configuration.Compute.MemorySize }}"
num_cpus: "{{ item.Configuration.Compute.CPUs }}"
num_cpu_cores_per_socket: "{{ item.Configuration.Compute.Cores }}"
scsi: "{{ item.Configuration.Storage.SCSIController }}"
networks:
- name: "{{ item.Configuration.Network.Portgroup }}"
device_type: "{{ item.Configuration.Network.NetworkAdapter }}"
cdrom:
- controller_number: 0
unit_number: 0
type: iso
iso_path: "[{{ Global.ISODatastore }}] {{ Global.ISODatastoreDir }}/{{ item.Name }}.iso"
state: present
annotation: |
*** Auto-Deployed by Ansible ***
Username: {{ item.Configuration.OS.User }}
Password: {{ item.Configuration.OS.Password }}
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Start checking if the Ubuntu VM(s) is/are ready
community.vmware.vmware_guest_info:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
datacenter: "{{ item.Placement.Datacenter }}"
validate_certs: false
name: "{{ item.Name }}"
schema: vsphere
register: vm_facts
until: vm_facts.instance.guest.hostName is search(item.Configuration.OS.Hostname)
retries: 30
delay: 60
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Set password for the Ubuntu user
community.vmware.vmware_vm_shell:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
vm_id: "{{ item.Name }}"
vm_username: "{{ item.Configuration.OS.User }}"
vm_password: VMware1!
vm_shell: /usr/bin/echo
vm_shell_args: "'{{ item.Configuration.OS.User }}:{{ item.Configuration.OS.Password }}' | sudo chpasswd"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Copy network configuration file(s) to working directory
ansible.builtin.template:
src: ./Ubuntu_Netplan.j2
dest: "{{ Global.Workingdir }}/{{ item.Name }}-00-installer-config.yaml"
mode: "755"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Copy network configuration file(s) to Ubuntu VM(s)
community.vmware.vmware_guest_file_operation:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
vm_id: "{{ item.Name }}"
vm_username: "{{ item.Configuration.OS.User }}"
vm_password: VMware1!
copy:
src: "{{ Global.Workingdir }}/{{ item.Name }}-00-installer-config.yaml"
dest: "/home/{{ item.Configuration.OS.User }}/{{ item.Name }}-00-installer-config.yaml"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Move network configuration file to correct location on Ubuntu VM(s)
community.vmware.vmware_vm_shell:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
vm_id: "{{ item.Name }}"
vm_username: "{{ item.Configuration.OS.User }}"
vm_password: VMware1!
vm_shell: /usr/bin/sudo
vm_shell_args: "mv /home/{{ item.Configuration.OS.User }}/{{ item.Name }}-00-installer-config.yaml /etc/netplan/00-installer-config.yaml"
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Appply the network configuration on Ubuntu VM(s)
community.vmware.vmware_vm_shell:
hostname: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
vm_id: "{{ item.Name }}"
vm_username: "{{ item.Configuration.OS.User }}"
vm_password: VMware1!
vm_shell: /usr/bin/sudo
vm_shell_args: netplan apply
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Delete custom Ubuntu ISO(s) from the Datastore
community.vmware.vsphere_file:
host: "{{ vCenter.FQDN }}"
username: "{{ vCenter.User }}"
password: "{{ vCenter.Password }}"
validate_certs: false
datacenter: "{{ Global.ISODatacenter }}"
datastore: "{{ Global.ISODatastore }}"
path: "{{ Global.ISODatastoreDir }}/{{ item.Name }}.iso"
state: absent
loop: "{{ VMsToDeploy }}"
when: item.Deploy
- name: Delete working directory on Ansible Controller
ansible.builtin.file:
path: "{{ Global.Workingdir }}"
state: absent