-
Notifications
You must be signed in to change notification settings - Fork 4
/
migrator-awx.yml
273 lines (246 loc) · 9.21 KB
/
migrator-awx.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
## This modified version of the playbook expects a variable "source_vm_name" to be
## provided. In turn, it will fetch info on that VM, and the host it currently
## resides on, to dynamically populate the inventory groups. Although this was
## intended for use in self-service migrations in AWX, it could also be useful to
## further simplify the migration process when running manually.
- name: Collect VM info from survey
hosts: localhost
gather_facts: false
vars_prompt:
- name: source_vm_name
prompt: What's the name of the VM in RHV?
private: no
tasks:
- name: Collect VM info from RHV
block:
- name: Obtain SSO token using username/password credentials
ovirt_auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
delegate_to: localhost
- name: Parse list of candidate VMs
include_tasks: fragments/parse_candidate_vms.yml
loop: "{{ source_vm_name.split(',') }}"
loop_control:
label: "{{ item }}"
always:
- name: Always revoke the SSO token
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
delegate_to: localhost
- name: Mount vmware_migration volume
hosts: rhvhost
become: true
gather_facts: true
tags: prepare_rhvh
tasks:
- name: Create a directory for the vmware_migration volume
ansible.builtin.file:
path: /mnt/vmware_migration
state: directory
mode: '0755'
- name: Mount vmware_migration volume locally to RHV host
mount:
src: "{{ nfs_mount_path }}"
path: /mnt/vmware_migration
boot: no
state: mounted
fstype: nfs
opts: vers=3
# - name: Ensure SSH is enabled on target ESXi
# hosts: localhost
# gather_facts: false
# tasks:
# - name: Start ssh setting for an ESXi Host
# community.vmware.vmware_host_service_manager:
# hostname: "{{ vcenter_endpoint }}"
# username: "{{ vcenter_username }}"
# password: "{{ vcenter_password }}"
# validate_certs: false
# esxi_hostname: "{{ groups['esxihost'][0] }}"
# service_name: ssh
# state: present
- name: Prepare source VM for migration
hosts: vmsToMigrate:!no_guest_preparation
become: true
gather_facts: true
tags:
- guest_preparation
tasks:
- name: Set "Preparing vSphere Migration" comment on VM
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
comment: "Preparing vSphere Migration"
id: "{{ rhv_data.id }}"
delegate_to: localhost
become: false
when: rhv_data.comment != "Preparing vSphere Migration"
- name: Run guest-specific migration preparation
ansible.builtin.include_role:
name: guest_preparation
- name: Build skeleton VM on vSphere
hosts: vmsToMigrate
gather_facts: false
connection: local
tasks:
- name: Collect VM specifications from RHV
block:
- name: Obtain SSO token with using username/password credentials
ovirt_auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
- ovirt.ovirt.ovirt_vm_info:
auth: "{{ ovirt_auth }}"
pattern: "{{ inventory_hostname }} and Status = up"
follows:
- disk_attachments.disk
- nics.vnic_profile.network
register: rhv_vm
- set_fact: vm_name="{{rhv_vm.ovirt_vms[0].name}}"
- set_fact:
rhv_vm_datacenter_id: "{{ rhv_data.cluster.data_center.id }}"
always:
- name: Always revoke the SSO token
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
- name: Create target VM in vCenter
community.vmware.vmware_guest:
hostname: "{{ vcenter_endpoint }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
folder: /Migrated Virtual Machines
datastore: "{{ vcenter_datastore }}"
datacenter: "{{ vcenter_datacenter }}"
cluster: "{{ vcenter_cluster }}"
name: "{{ rhv_data.name }}"
guest_id: "{{ (windows_mode | default(false)) | ternary('windows8Server64Guest','centos64Guest') }}"
hardware:
boot_firmware: bios
hotadd_cpu: yes
hotadd_memory: yes
# yOu HaVe A pRoBlEm WiTh PrEcIsIoN?
memory_mb: "{{ (rhv_data.memory / 1024 / 1024) | int }}"
num_cpus: "{{ rhv_data.cpu.topology.sockets }}"
scsi: "{{ (windows_mode | default(false)) | ternary('lsilogicsas', 'paravirtual') }}"
register: vmware_vm
- set_fact: vmware_vm_info="{{vmware_vm.instance}}"
- name: Associate VM network adapters with OS network adapters
set_fact:
transferring_nics: "{{ (transferring_nics | default([])) + [ { 'mac': item.mac.address, 'hv_name': item.name, 'os_name': ((((guest_nics | selectattr('mac', 'equalto', item.mac.address)) | list)[0].guest_dev) | default('ZZZ')), 'vlan': item.vnic_profile.network.vlan.id } ] }}"
loop: "{{ rhv_vm.ovirt_vms[0].nics }}"
loop_control:
#label: "{{ item.name }}"
label: "{{ item.mac.address }}"
tags:
- dest_nic_config
- name: Add network adapter to VM
community.vmware.vmware_guest_network:
hostname: "{{ vcenter_endpoint }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ vcenter_datacenter }}"
moid: "{{ vmware_vm_info.moid }}"
switch: "{{ vcenter_datacenter }}-dvswitch"
start_connected: yes
vlan_id: "{{ adapter.vlan }}"
loop: "{{ transferring_nics | sort(attribute='os_name') }}"
loop_control:
label: "{{adapter.hv_name}}"
index_var: loop_index
loop_var: adapter
tags:
- dest_nic_config
- name: The Main Event
hosts: vmsToMigrate
gather_facts: false
strategy: free
tasks:
- name: Start Migration
block:
- name: Annotate the source VM
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
comment: "Pending vSphere Migration"
id: "{{ rhv_data.id }}"
delegate_to: localhost
when: rhv_data.comment != "Pending vSphere Migration"
- name: Stop the source VM
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
state: stopped
id: "{{ rhv_data.id }}"
delegate_to: localhost
- name: Convert and Attach disks to target VM
include_tasks: fragments/convert_and_import_disk.yml
loop: "{{rhv_vm.ovirt_vms[0].disk_attachments | sort(attribute='disk.alias')}}"
loop_control:
label: "{{item.id}}"
index_var: loop_index
- name: Power up the target VM
community.vmware.vmware_guest:
hostname: "{{ vcenter_endpoint }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vmware_vm_info.hw_name }}"
state: poweredon
wait_for_ip_address: "{{ (windows_mode | default(false)) | ternary('no', 'yes') }}"
delegate_to: localhost
- name: Annotate the source VM
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
comment: "Migrated to vSphere"
id: "{{ rhv_data.id }}"
delegate_to: localhost
rescue:
- name: Start source VM due to migration failure
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
state: running
id: "{{ rhv_data.id }}"
delegate_to: localhost
- name: Annotate the source VM
ovirt.ovirt.ovirt_vm:
auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
comment: "Migration to vSphere failed"
name: "{{ vm_name }}"
when: rhv_data.comment != "Migration to vSphere failed"
delegate_to: localhost
- ansible.builtin.fail:
msg: "Something went wrong while attempting to migrate {{ vm_name }}, the source VM has been powered back up."
always:
- debug:
msg: "We would've removed the SSO token here"