This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
/
libvirt_nodepool.yml
260 lines (227 loc) · 7.86 KB
/
libvirt_nodepool.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
---
- name: check pub_key exists
stat:
path: "{{ pub_key }}"
register: stat_pub_key
- name: use ssh-agent first reported key if local pub_key not found
when: not stat_pub_key.stat.exists
block:
- name: dump first ssh-agent key
become: false
# ^ non false could break agent by causing:
# Could not open a connection to your authentication agent.
shell: |
ssh-add -L | head -1 > {{ pub_key }}.agent
test -s {{ pub_key }}.agent
# ^ fail if the file is empty
- name: reassign pub_key to point to ssh-agent key
set_fact:
pub_key: "{{ pub_key }}.agent"
- name: Fetch a CentOS image to use for libvirt nodepool nodes
include_role:
name: fetch-images
- name: Resize undercloud image (create target image)
command: >
qemu-img create -f qcow2 -o preallocation=off
'{{ working_dir }}/undercloud-resized.qcow2'
'80G'
- name: Resize undercloud image (call virt-resize)
command: >
virt-resize --expand /dev/sda1
'{{ working_dir }}/undercloud.qcow2'
'{{ working_dir }}/undercloud-resized.qcow2'
environment:
LIBGUESTFS_BACKEND: direct
LIBGUESTFS_DEBUG: 1
LIBGUESTFS_TRACE: 1
- name: Rename resized image to original name
command: >
mv -f '{{ working_dir }}/undercloud-resized.qcow2'
'{{ working_dir }}/undercloud.qcow2'
- name: Calculate password hash
no_log: true
shell: >
import crypt;
print crypt.crypt("{{ vm_pass }}", "$1$SecretSalt$")
args:
executable: /usr/bin/python
register: hash
- name: Copy generated password to file
copy:
content: "{{ hash.stdout }}"
dest: "{{ working_dir }}/pwtemp"
mode: 0600
- name: Inject password into the image
command: >
virt-customize -a {{ working_dir }}/undercloud.qcow2
--root-password file:"{{ working_dir }}/pwtemp"
environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
register: root_password
- name: Remove root password file
file:
path: "{{ working_dir }}/pwtemp"
state: absent
- name: Resize the filesystem
command: >
virt-customize -a {{ working_dir }}/undercloud.qcow2
--run-command 'xfs_growfs /'
environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
when: resize_qcow_filesystem|default(true)|bool
- name: Disable cloud-init
command: >
virt-customize -a {{ working_dir }}/undercloud.qcow2
--run-command "grubby --update-kernel=ALL --args=\"cloud-init=disabled\""
environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
- name: Inject ssh public key into the image
command: >
virt-customize -a {{ working_dir }}/undercloud.qcow2
--mkdir /root/.ssh/
--upload '{{ pub_key }}:/root/.ssh/authorized_keys'
--run-command 'chown -R root:root /root/.ssh'
--run-command 'chmod 0700 /root/.ssh'
--run-command 'chmod 0600 /root/.ssh/authorized_keys'
--selinux-relabel
environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
# note upstream images may be in use and have the zuul user created
- name: Add zuul user to the image
command: >
virt-customize -a {{ working_dir }}/undercloud.qcow2
--run-command 'id -u zuul &>/dev/null || useradd zuul'
--mkdir /home/zuul/.ssh
--run-command 'cp /root/.ssh/authorized_keys /home/zuul/.ssh/'
--run-command 'chown -R zuul:zuul /home/zuul/.ssh'
--run-command 'chmod 0700 /home/zuul/.ssh'
--run-command 'chmod 0600 /home/zuul/.ssh/authorized_keys'
--run-command 'echo "zuul ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/zuul'
--run-command 'chmod 0440 /etc/sudoers.d/zuul'
--selinux-relabel
# we should NOT create symlinks between python->python3
# see https://www.python.org/dev/peps/pep-0394/
# --run-command 'ln -s /usr/bin/python3 /usr/bin/python'
environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
- debug:
msg: "Add basic packages we need to the image"
- environment:
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
block:
# Create libvirt volumes and upload them to libvirt.
- name: Create libvirt nodepool volumes
command: >
virsh vol-create-as {{ libvirt_volume_pool }}
{{ item.name }}.qcow2
{{ flavors[item.flavor].disk }}G --format qcow2
with_items: "{{ overcloud_nodes }}"
- name: Upload the volume to storage pool
command: >
virsh -k 0 vol-upload --pool '{{ libvirt_volume_pool }}'
'{{ item.name }}.qcow2'
'{{ local_working_dir }}/undercloud.qcow2'
async: 600
poll: 10
with_items: "{{ overcloud_nodes }}"
- name: Start libvirt nodepool nodes
virt:
name: "{{ item.name }}"
command: start
autostart: true
state: running
uri: "{{ libvirt_uri }}"
with_items: "{{ overcloud_nodes }}"
## need to find a way to make these next tasks generic
- name: Get libvirt nodepool IP addresses
script: "get-domain-ip.sh subnode-0"
register: "subnode_0_ip_result"
until: "subnode_0_ip_result is success"
retries: 20
delay: 10
- name: Set_fact for undercloud ip
set_fact:
subnode_0_ip: "{{ subnode_0_ip_result.stdout_lines[0] }}"
cacheable: true
- name: Wait until ssh is available
wait_for:
host: "{{ subnode_0_ip }}"
state: started
port: 22
timeout: 600
- name: Add subnode-0 to inventory
add_host:
name: subnode-0
groups: subnodes
ansible_host: "{{ subnode_0_ip }}"
ansible_fqdn: "{{ subnode_0_ip }}"
ansible_user: zuul
subnode_private_ip: "{{ subnode_0_ip }}"
subnode_public_ip: "{{ subnode_0_ip }}"
ansible_python_interpreter: "{{ python_interpreter|default('/usr/bin/python') }}"
- name: Add subnode-0 to known_hosts
known_hosts:
name: "{{ subnode_0_ip }}"
key: "{{ lookup('pipe', 'ssh-keyscan
-t ecdsa-sha2-nistp256 ' + subnode_0_ip) }}"
- name: Set hostname correctly for subnode-0
delegate_to: subnode-0
shell: >
echo "127.0.0.1 subnode-0 localhost" > /etc/hosts;
echo "HOSTNAME=subnode-0" >> /etc/sysconfig/network;
echo "subnode-0" > /etc/hostname;
hostnamectl set-hostname subnode-0;
echo "nameserver {{ custom_nameserver|default('8.8.8.8') }} " >> /etc/resolv.conf;
echo "append domain-name-servers {{ custom_nameserver|default('8.8.8.8') }};" >> /etc/dhcp/dhclient.conf
become: true
- name: Get libvirt nodepool IP addresses
script: "get-domain-ip.sh subnode-1"
register: "subnode_1_ip_result"
until: "subnode_1_ip_result is success"
retries: 20
delay: 10
- name: Set_fact for undercloud ip
set_fact:
subnode_1_ip: "{{ subnode_1_ip_result.stdout_lines[0] }}"
cacheable: true
- name: Wait until ssh is available
wait_for:
host: "{{ subnode_1_ip }}"
state: started
port: 22
timeout: 600
- name: Add subnode-1 to inventory
add_host:
name: subnode-1
groups: subnodes
ansible_host: "{{ subnode_1_ip }}"
ansible_fqdn: "{{ subnode_1_ip }}"
ansible_user: zuul
subnode_private_ip: "{{ subnode_1_ip }}"
subnode_public_ip: "{{ subnode_1_ip }}"
ansible_python_interpreter: "{{ python_interpreter|default('/usr/bin/python') }}"
- name: Add subnode-1 to known_hosts
known_hosts:
name: "{{ subnode_1_ip }}"
key: "{{ lookup('pipe', 'ssh-keyscan
-t ecdsa-sha2-nistp256 ' + subnode_1_ip) }}"
- name: Set hostname correctly for subnode-1
delegate_to: subnode-1
shell: >
echo "127.0.0.1 subnode-1 localhost" > /etc/hosts;
echo "HOSTNAME=subnode-1" >> /etc/sysconfig/network;
echo "subnode-1" > /etc/hostname;
hostnamectl set-hostname subnode-1;
echo "nameserver {{ custom_nameserver|default('8.8.8.8') }} " >> /etc/resolv.conf;
echo "append domain-name-servers {{ custom_nameserver|default('8.8.8.8') }};" >> /etc/dhcp/dhclient.conf
become: true
- name: Create inventory suitable for zuul-jobs/multinode
template:
src: "{{ zuul_hosts_template }}"
dest: "{{ local_working_dir }}/zuul-hosts.yaml"
mode: 0755