forked from openstack-k8s-operators/ci-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_layout.yml
More file actions
355 lines (324 loc) · 11.1 KB
/
deploy_layout.yml
File metadata and controls
355 lines (324 loc) · 11.1 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
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
---
- name: Import layout generator if needed
when:
- _cifmw_libvirt_manager_layout is not defined
ansible.builtin.include_tasks: generate_layout.yml
# TODO(hjensas): We should have a dedicated role for firewall
- name: Enable forwarding in the libvirt zone
when: cifmw_libvirt_manager_firewalld_zone_libvirt_forward | default(true) | bool
become: true
notify: Restart firewalld
ansible.builtin.command:
cmd: >-
firewall-cmd --permanent --zone libvirt --add-forward
- name: Enable masquerading for public traffic
when: cifmw_libvirt_manager_firewalld_default_zone_masquerade | default(true) | bool
become: true
ansible.posix.firewalld:
masquerade: true
zone: "{{ cifmw_libvirt_manager_firewalld_default_zone }}"
permanent: true
immediate: true
state: enabled
- name: Ensure firewalld is restarts
ansible.builtin.meta: flush_handlers
# todo(Lewis): We need to deploy VirtualBMC on the controller rather than hypervisor
# Deploy VBMC only on the hypervisor running OpenShift services,
# being OCP cluster or single CRC.
- name: Deploy VirtualBMC service container
tags:
- bootstrap
- bootstrap_layout
when:
- cifmw_use_vbmc | default(false) | bool
- (_cifmw_libvirt_manager_layout.vms.crc.target is defined and
_cifmw_libvirt_manager_layout.vms.crc.target == inventory_hostname) or
(_cifmw_libvirt_manager_layout.vms.crc is defined and
_cifmw_libvirt_manager_layout.vms.crc.target is undefined) or
(_cifmw_libvirt_manager_layout.vms.ocp.target is defined and
_cifmw_libvirt_manager_layout.vms.ocp.target == inventory_hostname) or
(_cifmw_libvirt_manager_layout.vms.ocp is defined and
_cifmw_libvirt_manager_layout.vms.ocp.target is undefined) or
(_cifmw_libvirt_manager_layout.vms.crc is undefined and _cifmw_libvirt_manager_layout.vms.ocp is undefined)
block:
- name: Deploy virtualbmc
ansible.builtin.include_role:
name: virtualbmc
- name: Let the project know we have vbmc available
ansible.builtin.set_fact:
_vbmc_available: true
_vbmc_host: "{{ inventory_hostname }}"
- name: Create needed workload directory
ansible.builtin.file:
path: "{{ cifmw_libvirt_manager_basedir }}/{{ item }}"
state: directory
mode: "0755"
loop:
- workload
- reproducer-inventory
- volumes
- name: Allow QEMU on workload directory
become: true
ansible.posix.acl:
path: "{{ cifmw_libvirt_manager_basedir }}/{{ item }}"
entity: "qemu"
etype: "user"
permissions: rwx
state: present
loop:
- workload
- volumes
- name: Generate networking data
when:
- cifmw_libvirt_manager_mac_map is undefined
ansible.builtin.include_tasks: generate_networking_data.yml
- name: Create group inventories
when:
- _cifmw_libvirt_manager_layout.vms[item].manage | default(true)
- _cifmw_libvirt_manager_layout.vms[item].amount | default(1) | int > 0
vars:
hosts: >-
{{
cifmw_libvirt_manager_all_vms |
dict2items |
selectattr('value', 'equalto', item)
}}
ansible.builtin.template:
dest: "{{ cifmw_libvirt_manager_basedir }}/reproducer-inventory/{{ item }}-group.yml"
src: inventory.yml.j2
mode: "0644"
loop: "{{ _cifmw_libvirt_manager_layout.vms.keys() }}"
loop_control:
label: "{{ item }}"
- name: Create "all" group inventory file
ansible.builtin.include_tasks: create_all_group_inventory.yml
- name: Ensure storage pool is present.
when:
- (require_extra_disks | int) > 0
vars:
pool_action: "create"
_extra_disks: >-
{{
_cifmw_libvirt_manager_layout.vms |
dict2items |
selectattr('value.extra_disks_num', 'defined') |
items2dict
}}
require_extra_disks: "{{ _extra_disks | length }}"
ansible.builtin.include_tasks: storage_pool.yml
- name: Ensure images are present
when:
- item.key is not match('.*(ocp|crc).*')
vars:
image_data: "{{ item.value }}"
ansible.builtin.include_tasks:
file: get_image.yml
loop: "{{ _cifmw_libvirt_manager_layout.vms | dict2items }}"
loop_control:
label: "{{ item.key }}"
- name: Manage SSH key
vars:
_sshkey: "{{ ansible_user_dir }}/.ssh/cifmw_reproducer_key"
block:
- name: Get ssh key state
register: _keystat
ansible.builtin.stat:
path: "{{ _sshkey }}"
- name: Create temporary ssh keypair
when:
- not _keystat.stat.exists
community.crypto.openssh_keypair:
path: "{{ _sshkey }}"
type: "{{ cifmw_libvirt_manager_reproducer_key_type }}"
size: "{{ cifmw_libvirt_manager_reproducer_key_size }}"
comment: cifmw_reproducer_key
- name: Slurp public key for later use
register: pub_ssh_key
ansible.builtin.slurp:
path: "{{ _sshkey }}.pub"
- name: Slurp private key for later use
register: priv_ssh_key
ansible.builtin.slurp:
path: "{{ _sshkey }}"
- name: Inject cifmw_reproducer_key.pub in hypervisor authorized_keys
ansible.posix.authorized_key:
user: "{{ ansible_user_id }}"
key: "{{ pub_ssh_key['content'] | b64decode }}"
- name: Create fact holding network data for VMs
ansible.builtin.set_fact:
cacheable: false
cifmw_reproducer_network_data: {}
- name: List existing networks
register: _virt_nets
community.libvirt.virt_net:
command: "list_nets"
- name: Gather pool fact
when:
- ansible_libvirt_pools is undefined
community.libvirt.virt_pool:
command: facts
- name: Create and run VMs
when:
- (
vm_data.value.target is defined and
vm_data.value.target == inventory_hostname
) or
vm_data.value.target is undefined
- (vm_data.value.amount is defined and
(vm_data.value.amount | int) > 0) or
vm_data.value.amount is undefined
vars:
vm_type: "{{ _vm.value }}"
vm: "{{ _vm.key }}"
vm_data: >-
{{
_cifmw_libvirt_manager_layout.vms[vm_type]
}}
pub_key: "{{ pub_ssh_key.content | b64decode }}"
priv_key: "{{ priv_ssh_key.content | b64decode }}"
_vm_img: >-
{{ vm }}.qcow2
ansible.builtin.include_tasks:
file: create_vms.yml
loop: "{{ cifmw_libvirt_manager_all_vms | dict2items }}"
loop_control:
loop_var: _vm
index_var: vm_idx
- name: "Start (power-on) VMs"
ansible.builtin.include_tasks: start_vms.yml
- name: "Configure managed VMs"
when:
- vm_data.manage | default(true) | bool
- vm_data.disk_file_name != 'blank'
vars:
vm_type: "{{ _vm.value }}"
vm: "{{ _vm.key }}"
vm_data: >-
{{
_cifmw_libvirt_manager_layout.vms[vm_type]
}}
_init_admin_user: "{{ vm_data.value.admin_user | default('root') }}"
pub_key: "{{ pub_ssh_key.content | b64decode }}"
priv_key: "{{ priv_ssh_key.content | b64decode }}"
vm_con_name: "{{ vm | replace('ocp-', '') }}.{{ inventory_hostname }}"
ansible.builtin.include_tasks: manage_vms.yml
loop: "{{ cifmw_libvirt_manager_all_vms | dict2items }}"
loop_control:
loop_var: _vm
- name: Create VBMC entity
when:
- _vbmc_available is defined
- _vbmc_available | bool
vars:
cifmw_virtualbmc_machine: "cifmw-{{ item.key }}"
cifmw_virtualbmc_ipmi_port: >-
{{
cifmw_virtualbmc_ipmi_base_port + index
}}
cifmw_virtualbmc_action: "add"
_ipmi_host: >-
{%- if _cifmw_libvirt_manager_layout.vms.crc.target is defined -%}
{{_cifmw_libvirt_manager_layout_layout.vms.crc.target }}
{%- elif _cifmw_libvirt_manager_layout.vms.ocp.target is defined -%}
{{ _cifmw_libvirt_manager_layout.vms.ocp.target }}
{%- else -%}
{{ inventory_hostname }}
{%- endif -%}
cifmw_virtualbmc_ipmi_address: "{{ _ipmi_host }}.utility"
ansible.builtin.include_role:
name: virtualbmc
tasks_from: manage_host
loop: "{{ _libvirt_manager_networking.instances | dict2items }}"
loop_control:
index_var: index
label: "{{ item.key }}"
- name: List running virtual machines.
community.libvirt.virt:
command: list_vms
uri: "qemu:///system"
register: _vms
- name: Dump UUIDs
when:
- cifmw_libvirt_manager_uuids is defined
- cifmw_libvirt_manager_uuids | length > 0
vars:
_content:
libvirt_uuid: "{{ cifmw_libvirt_manager_uuids }}"
ansible.builtin.copy:
dest: "{{ cifmw_libvirt_manager_basedir }}/artifacts/libvirt-uuids.yml"
content: "{{ _content | to_nice_yaml }}"
mode: "0644"
- name: Refresh and dump vbmc hosts
when:
- cifmw_use_vbmc | default(false) | bool
- _vbmc_host is defined
- _vbmc_host == inventory_hostname
block:
- name: Ensure fresh vbmc listing
ansible.builtin.include_role:
name: virtualbmc
tasks_from: list_hosts.yml
- name: Update vbmc related fact
vars:
_auth:
username: "{{ cifmw_virtualbmc_ipmi_user | default('admin') }}"
password: "{{ cifmw_virtualbmc_ipmi_password | default('password') }}"
_map_auth: >-
{{
cifmw_virtualbmc_known_hosts |
map('combine', _auth)
}}
_map_uuid: >-
{% set ns = namespace(output=[]) -%}
{% for host in _map_auth -%}
{% set _uuid = {'uuid': cifmw_libvirt_manager_uuids[host['Domain name']]} -%}
{% set _host = host | combine(_uuid) -%}
{% set _ = ns.output.append(_host) -%}
{% endfor -%}
{{ ns.output }}
ansible.builtin.set_fact:
cifmw_virtualbmc_known_hosts: "{{ _map_uuid }}"
- name: Dump vbmc known hosts
vars:
content:
cifmw_virtualbmc_known_hosts: "{{ cifmw_virtualbmc_known_hosts }}"
ansible.builtin.copy:
dest: >-
{{ cifmw_libvirt_manager_basedir }}/artifacts/virtual-nodes.yml
content: "{{ content | to_nice_yaml }}"
mode: "0644"
- name: Ensure we get proper access to CRC
when:
- _cifmw_libvirt_manager_layout.vms.crc is defined
- (_cifmw_libvirt_manager_layout.vms.crc.amount is defined and
(_cifmw_libvirt_manager_layout.vms.crc.amount | int) > 0) or
_cifmw_libvirt_manager_layout.vms.crc.amount is undefined
- (
_cifmw_libvirt_manager_layout.vms.crc.target is defined and
_cifmw_libvirt_manager_layout.vms.crc.target == inventory_hostname
) or
_cifmw_libvirt_manager_layout.vms.crc.target is undefined
vars:
crc_private_key: >-
{{
(_cifmw_libvirt_manager_layout.vms.crc.image_local_dir, "id_ecdsa") |
path_join
}}
block:
- name: Copy authorized_keys
ansible.builtin.shell:
cmd: >-
set -xe -o pipefail;
ssh -i {{ crc_private_key}} core@crc-0 "mkdir -p .ssh";
cat ~/.ssh/authorized_keys | ssh -i {{ crc_private_key }} core@crc-0 "cat >> .ssh/authorized_keys"
- name: Enable root access on CRC
ansible.builtin.shell:
cmd: |-
set -xe -o pipefail
cat << EOF | ssh -i {{ crc_private_key }} core@crc-0
sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config.d/40-rhcos-defaults.conf;
sudo systemctl restart sshd;
sudo cp -r /home/core/.ssh/authorized_keys* /root/.ssh;
sudo chown -R root: /root/.ssh;
sudo chmod 0750 /root/.ssh;
EOF