forked from cwilloughby-bw/rhv-2-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator.yml
228 lines (202 loc) · 7.74 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
- 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: Collect common data 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
delegate_to: localhost
- ovirt.ovirt.ovirt_vnic_profile_info:
auth: "{{ ovirt_auth }}"
register: rhv_vnic_profiles
delegate_to: localhost
- set_fact: rhv_vnic_profiles="{{ rhv_vnic_profiles }}"
always:
- name: Always revoke the SSO token
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
delegate_to: localhost
- name: Ensure source VM has vmware-tools installed
hosts: vmsToMigrate
become: true
gather_facts: false
tasks:
- name: Install open-vm-tools on source VM
ansible.builtin.yum:
name: open-vm-tools
state: latest
## These are a few ugly shell commands, I'd like to move these to a more proper ansible fashion
## For this shell command specifically, I can't use copy due to the env vars, afaik. Could probably
## pull that from the facts??
- name: Backup existing initramfs
ansible.builtin.shell: cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img-old
- name: Reconfigure initramfs to support VMWare Paravirtual + LSILogic controllers
ansible.builtin.shell: dracut -f -v --add-drivers "mptspi scsi_transport_spi mptsas mptscsih mptbase scsi_transport_sas vmw_pvscsi" /boot/initramfs-$(uname -r).img $(uname -r)
## Not gonna lie, this is pretty jank/lazy. On every attempt, the source VMs main interface was eth0,
## and the target ended up having ens192. Sooooo, we just copy the config, and change the target
## interface name. This only sets up the first NIC, additional NICs will need someone to manually
## perform that process.
- name: Copy existing network configuration
ansible.builtin.copy:
remote_src: yes
src: /etc/sysconfig/network-scripts/ifcfg-eth0
dest: /etc/sysconfig/network-scripts/ifcfg-ens192
## inb4 Bitt roasts me for not using an actual RegEx here
- name: Update network adapter name in new configuration
ansible.builtin.replace:
path: /etc/sysconfig/network-scripts/ifcfg-ens192
regexp: eth0
replace: ens192
- 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 }}"
next_run: true
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
- ovirt.ovirt.ovirt_disk_info:
auth: "{{ ovirt_auth }}"
pattern: "{{ rhv_vm.ovirt_vms[0].name }}"
register: rhv_vm_disks
- set_fact: rhv_vm_disk_info="{{rhv_vm_disks.ovirt_disks}}"
## 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: Configure target VM network adapters
include_tasks: fragments/configure_network_adapters.yml
loop: "{{ rhv_vm_nics.ovirt_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: Obtain RHV SSO token with using username/password credentials
ovirt_auth:
url: "{{ rhv_endpoint }}"
username: "{{ rhv_username }}@{{ rhv_domain }}"
password: "{{ rhv_password }}"
insecure: true
delegate_to: localhost
- name: Stop the source VM
ovirt.ovirt.ovirt_vm:
auth: "{{ ovirt_auth }}"
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: "{{ ovirt_auth }}"
comment: "Migrated to vSphere"
name: "{{ vm_name }}"
delegate_to: localhost
rescue:
- name: Start source VM due to migration failure
ovirt.ovirt.ovirt_vm:
auth: "{{ ovirt_auth }}"
state: running
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:
- name: Always revoke the RHV SSO token
ovirt_auth:
state: absent
ovirt_auth: "{{ ovirt_auth }}"
delegate_to: localhost