-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmigrator.yml
204 lines (187 loc) · 6.61 KB
/
migrator.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
- name: Mount vmware_migration volume
hosts: rhvhost
become: true
gather_facts: false
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: Prepare source VM for migration
hosts: vmsToMigrate:!no_guest_preparation
become: true
gather_facts: true
tasks:
- 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 }}"
follows:
- disk_attachments.disk
- nics.vnic_profile.network
register: rhv_vm
- set_fact: vm_name="{{rhv_vm.ovirt_vms[0].name}}"
- ovirt.ovirt.ovirt_nic_info:
auth: "{{ ovirt_auth }}"
vm: "{{ rhv_vm.ovirt_vms[0].name }}"
name: "*"
fetch_nested: yes
nested_attributes:
- vnic_profiles
register: rhv_vm_nics
## All this, just to get the datacenter ID, which should've just been included in the VM response
- ovirt.ovirt.ovirt_cluster_info:
auth: "{{ ovirt_auth }}"
register: rhv_clusters
- set_fact: rhv_vm_datacenter_id="{{item.data_center.id}}"
loop: "{{ rhv_clusters.ovirt_clusters }}"
loop_control:
label: "{{item.name}}"
when: item.id == rhv_vm.ovirt_vms[0].cluster.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_vm.ovirt_vms[0].name }}"
guest_id: centos64Guest
hardware:
boot_firmware: bios
hotadd_cpu: yes
hotadd_memory: yes
# yOu HaVe A pRoBlEm WiTh PrEcIsIoN?
memory_mb: "{{ (rhv_vm.ovirt_vms[0].memory / 1024 / 1024) | int }}"
num_cpus: "{{ rhv_vm.ovirt_vms[0].cpu.topology.sockets }}"
scsi: paravirtual
register: vmware_vm
- set_fact: vmware_vm_info="{{vmware_vm.instance}}"
- 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.vnic_profile.network.vlan.id }}"
loop: "{{ rhv_vm.ovirt_vms[0].nics }}"
loop_control:
label: "{{adapter.name}}"
index_var: loop_index
loop_var: adapter
- 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"
name: "{{ vm_name }}"
delegate_to: localhost
- 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
name: "{{ vm_name }}"
delegate_to: localhost
- name: Convert and Attach disks to target VM
include_tasks: fragments/convert_and_import_disk.yml
loop: "{{rhv_vm_disk_info}}"
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: 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"
name: "{{ vm_name }}"
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
name: "{{ vm_name }}"
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 }}"
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"