tripleo-quickstart/roles/libvirt/setup/overcloud/tasks/libvirt_nodepool.yml

194 lines
5.9 KiB
YAML

---
- 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'
changed_when: true
- name: Test libguestfs
shell: libguestfs-test-tool > {{ working_dir }}/libguestfs-test.log 2>&1;
ignore_errors: true
register: libguestfs
changed_when: false
- name: Compose libguestfs env vars
template:
src: libguestfs-env.sh.j2
dest: "{{ working_dir }}/libguestf-env.sh"
- name: Resize undercloud image (call virt-resize)
shell: >
set -eo pipefail;
source {{ working_dir }}/libguestf-env.sh;
virt-resize -x --verbose --expand /dev/sda1
'{{ working_dir }}/undercloud.qcow2'
'{{ working_dir }}/undercloud-resized.qcow2'
2>&1 | tee -a {{ working_dir }}/virt-resize.log
register: result
no_log: result is success
changed_when: true
- name: Rename resized image to original name
command: >
mv -f '{{ working_dir }}/undercloud-resized.qcow2'
'{{ working_dir }}/undercloud.qcow2'
changed_when: true
- name: Calculate password hash
no_log: true
shell: >
import crypt;
print crypt.crypt("{{ vm_pass }}", "$1$SecretSalt$")
args:
executable: /usr/bin/python
register: hash
changed_when: false
- name: Copy generated password to file
copy:
content: "{{ hash.stdout }}"
dest: "{{ working_dir }}/pwtemp"
mode: 0600
- name: Inject password into the image
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -v -a {{ working_dir }}/undercloud.qcow2
--root-password file:"{{ working_dir }}/pwtemp"
>> {{ working_dir }}/virt-customize.log 2>&1
register: root_password
changed_when: true
- name: Remove root password file
file:
path: "{{ working_dir }}/pwtemp"
state: absent
- name: Resize the filesystem
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -v -a {{ working_dir }}/undercloud.qcow2
--run-command 'xfs_growfs /'
>> {{ working_dir }}/virt-customize.log 2>&1
when: resize_qcow_filesystem|default(true)|bool
changed_when: true
- name: Disable cloud-init
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -v -a {{ working_dir }}/undercloud.qcow2
--run-command "grubby --update-kernel=ALL --args=\"cloud-init=disabled\""
>> {{ working_dir }}/virt-customize.log 2>&1
changed_when: true
- name: Inject ssh public key into the image
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -v -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
>> {{ working_dir }}/virt-customize.log 2>&1
changed_when: true
# note upstream images may be in use and have the zuul user created
- name: Add zuul user to the image
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -v -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
>> {{ working_dir }}/virt-customize.log 2>&1
# 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'
changed_when: true
- name: Install and enable qemu-guest-agent
shell: >
source {{ working_dir }}/libguestf-env.sh &&
virt-customize -a {{ working_dir }}/undercloud.qcow2
--run-command 'yum install -y qemu-guest-agent'
--run-command 'systemctl enable qemu-guest-agent'
>> {{ working_dir }}/virt-customize.log 2>&1
changed_when: true
- 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 }}"
- include_tasks: register_vm.yml
with_items: "{{ overcloud_nodes }}"
- name: Create inventory suitable for zuul-jobs/multinode
template:
src: "{{ zuul_hosts_template }}"
dest: "{{ local_working_dir }}/zuul-hosts.yaml"
mode: 0755