diff options
author | Christian Pointner <equinox@spreadspace.org> | 2020-11-21 03:42:54 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2020-11-21 03:42:54 +0100 |
commit | 8828d4e11c28da88d65e45014431361aabb0c3d5 (patch) | |
tree | 759456a48196e108cf35bab4f1c6a774e8c20170 /roles/vm/guest | |
parent | add some vmhost test machines (diff) |
use delegation for vm/guest install and define
Diffstat (limited to 'roles/vm/guest')
-rw-r--r-- | roles/vm/guest/define/defaults/main.yml | 2 | ||||
-rw-r--r-- | roles/vm/guest/define/tasks/main.yml | 49 | ||||
-rw-r--r-- | roles/vm/guest/define/templates/libvirt-domain.xml.j2 | 18 | ||||
-rw-r--r-- | roles/vm/guest/install/tasks/installer-debian.yml | 8 | ||||
-rw-r--r-- | roles/vm/guest/install/tasks/installer-openbsd.yml | 8 | ||||
-rw-r--r-- | roles/vm/guest/install/tasks/main.yml | 97 |
6 files changed, 98 insertions, 84 deletions
diff --git a/roles/vm/guest/define/defaults/main.yml b/roles/vm/guest/define/defaults/main.yml index f0bcc4fd..05aeaab6 100644 --- a/roles/vm/guest/define/defaults/main.yml +++ b/roles/vm/guest/define/defaults/main.yml @@ -1,5 +1,5 @@ --- -vm_define_autostart: "{{ not vm_define_installer and hostvars[install_hostname].install_cooked.vm.autostart | default(False) }}" +vm_define_autostart: "{{ not vm_define_installer and install_cooked.vm.autostart | default(False) }}" vm_define_start: yes vm_define_installer: no diff --git a/roles/vm/guest/define/tasks/main.yml b/roles/vm/guest/define/tasks/main.yml index d0790628..65094bbb 100644 --- a/roles/vm/guest/define/tasks/main.yml +++ b/roles/vm/guest/define/tasks/main.yml @@ -1,5 +1,6 @@ --- - name: check if vm already exists + delegate_to: "{{ vm_host_cooked.name }}" virt: name: "{{ install_hostname }}" command: info @@ -7,43 +8,47 @@ - name: remove old vm when: install_hostname in vmhost_info + delegate_to: "{{ vm_host_cooked.name }}" block: - - name: destroy exisiting vm - virt: - name: "{{ install_hostname }}" - state: destroyed + - name: destroy exisiting vm + virt: + name: "{{ install_hostname }}" + state: destroyed - - name: wait for vm to be destroyed - wait_for_virt: - name: "{{ install_hostname }}" - states: shutdown,crashed - timeout: 5 + - name: wait for vm to be destroyed + wait_for_virt: + name: "{{ install_hostname }}" + states: shutdown,crashed + timeout: 5 - - name: undefining exisiting vm - virt: - name: "{{ install_hostname }}" - command: undefine + - name: undefining exisiting vm + virt: + name: "{{ install_hostname }}" + command: undefine - name: define vm + delegate_to: "{{ vm_host_cooked.name }}" virt: command: define xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" - name: start new vm when: vm_define_start | bool + delegate_to: "{{ vm_host_cooked.name }}" block: - - name: start vm - virt: - name: "{{ install_hostname }}" - state: running + - name: start vm + virt: + name: "{{ install_hostname }}" + state: running - - name: wait for VM to start - wait_for_virt: - name: "{{ install_hostname }}" - states: running - timeout: 10 + - name: wait for VM to start + wait_for_virt: + name: "{{ install_hostname }}" + states: running + timeout: 10 - name: mark vm as autostarted + delegate_to: "{{ vm_host_cooked.name }}" virt: name: "{{ install_hostname }}" autostart: "{{ vm_define_autostart }}" diff --git a/roles/vm/guest/define/templates/libvirt-domain.xml.j2 b/roles/vm/guest/define/templates/libvirt-domain.xml.j2 index 05ad6750..ce8a48d4 100644 --- a/roles/vm/guest/define/templates/libvirt-domain.xml.j2 +++ b/roles/vm/guest/define/templates/libvirt-domain.xml.j2 @@ -1,8 +1,8 @@ <domain type='kvm'> <name>{{ install_hostname }}</name> - <memory>{{ ((hostvars[install_hostname].install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</memory> - <currentMemory>{{ ((hostvars[install_hostname].install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</currentMemory> - <vcpu>{{ hostvars[install_hostname].install_cooked.vm.numcpus }}</vcpu> + <memory>{{ ((install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</memory> + <currentMemory>{{ ((install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</currentMemory> + <vcpu>{{ install_cooked.vm.numcpus }}</vcpu> <os> <type arch='x86_64' machine='pc-0.12'>hvm</type> {% if vm_define_installer %} @@ -50,8 +50,8 @@ </disk> {% endif %} -{% if 'virtio' in hostvars[install_hostname].install_cooked.disks %} -{% for device, src in hostvars[install_hostname].install_cooked.disks.virtio.items() %} +{% if 'virtio' in install_cooked.disks %} +{% for device, src in install_cooked.disks.virtio.items() %} <disk type='block' device='disk'> <driver name='qemu' type='raw' cache='none' discard='unmap'/> {% if src.type == 'lvm' %} @@ -64,9 +64,9 @@ {% endfor %} {% endif %} -{% if 'scsi' in hostvars[install_hostname].install_cooked.disks %} +{% if 'scsi' in install_cooked.disks %} <controller type='scsi' index='0' model='virtio-scsi'/> -{% for device, src in hostvars[install_hostname].install_cooked.disks.scsi.items() %} +{% for device, src in install_cooked.disks.scsi.items() %} <disk type='block' device='disk'> <driver name='qemu' type='raw' cache='none' discard='unmap'/> {% if src.type == 'lvm' %} @@ -79,8 +79,8 @@ {% endfor %} {% endif %} -{% if hostvars[install_hostname].install_cooked.interfaces %} -{% for if in hostvars[install_hostname].install_cooked.interfaces %} +{% if install_cooked.interfaces %} +{% for if in install_cooked.interfaces %} <interface type='bridge'> {% if 'mac' in if %} <mac address='{{ if.mac }}'/> diff --git a/roles/vm/guest/install/tasks/installer-debian.yml b/roles/vm/guest/install/tasks/installer-debian.yml index b0bc6e9e..9e484b88 100644 --- a/roles/vm/guest/install/tasks/installer-debian.yml +++ b/roles/vm/guest/install/tasks/installer-debian.yml @@ -1,17 +1,19 @@ --- +## this file is sourced using import_task and all task are therefore delegated to the vm-host - name: fetch debian installer files vars: + installer_base_path: "{{ hostvars[vm_host_cooked.name].installer_base_path }}" debian_installer_distro: "{{ install_distro }}" debian_installer_codename: "{{ install_codename }}" - debian_installer_arch: "{{ hostvars[install_hostname].install_cooked.arch | default('amd64') }}" + debian_installer_arch: "{{ install_cooked.arch | default('amd64') }}" debian_installer_variant: netboot import_role: name: installer/debian/fetch - name: copy the installer files to the temporary installer directory loop: - - linux - - initrd.gz + - linux + - initrd.gz copy: remote_src: yes src: "{{ debian_installer_target_dir }}/{{ item }}" diff --git a/roles/vm/guest/install/tasks/installer-openbsd.yml b/roles/vm/guest/install/tasks/installer-openbsd.yml index cc59ee36..903e94e2 100644 --- a/roles/vm/guest/install/tasks/installer-openbsd.yml +++ b/roles/vm/guest/install/tasks/installer-openbsd.yml @@ -1,17 +1,19 @@ --- +## this file is sourced using import_task and all task are therefore delegated to the vm-host - name: fetch openbsd installer files vars: + installer_base_path: "{{ hostvars[vm_host_cooked.name].installer_base_path }}" openbsd_installer_version: "{{ install_codename }}" - openbsd_installer_arch: "{{ hostvars[install_hostname].install_cooked.arch | default('amd64') }}" + openbsd_installer_arch: "{{ install_cooked.arch | default('amd64') }}" import_role: name: installer/openbsd/fetch - name: generate host specific autoinstall iso vars: - obsd_autoinstall_orig_iso: "{{ installer_base_path }}/openbsd-{{ install_codename }}/{{ hostvars[install_hostname].install_cooked.arch | default('amd64') }}/install{{ openbsd_installer_version_short }}.iso" + obsd_autoinstall_orig_iso: "{{ hostvars[vm_host_cooked.name].installer_base_path }}/openbsd-{{ install_codename }}/{{ install_cooked.arch | default('amd64') }}/install{{ openbsd_installer_version_short }}.iso" obsd_autoinstall_tmpdir: "{{ tmpdir.path }}" obsd_autoinstall_version: "{{ install_codename }}" - obsd_autoinstall_arch: "{{ hostvars[install_hostname].install_cooked.arch | default('amd64') }}" + obsd_autoinstall_arch: "{{ install_cooked.arch | default('amd64') }}" obsd_autoinstall_serial_device: com0 install_interface: vio0 import_role: diff --git a/roles/vm/guest/install/tasks/main.yml b/roles/vm/guest/install/tasks/main.yml index cf1286c5..0172c5bb 100644 --- a/roles/vm/guest/install/tasks/main.yml +++ b/roles/vm/guest/install/tasks/main.yml @@ -1,8 +1,9 @@ --- - name: create lvm-based disks for vm - loop: "{{ hostvars[install_hostname].install_cooked.disks.virtio | default({}) | combine(hostvars[install_hostname].install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'lvm') | list }}" + loop: "{{ install_cooked.disks.virtio | default({}) | combine(install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'lvm') | list }}" loop_control: label: "{{ item.value.vg }} / {{ item.value.lv }} ({{ item.value.size }})" + delegate_to: "{{ vm_host_cooked.name }}" lvol: vg: "{{ item.value.vg }}" lv: "{{ item.value.lv }}" @@ -10,7 +11,8 @@ state: present - name: create zfs base datasets for vm - loop: "{{ hostvars[install_hostname].install_cooked.disks.virtio | default({}) | combine(hostvars[install_hostname].install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'zfs') | map(attribute='value.backend') | map('default', 'default') | unique | list }}" + loop: "{{ install_cooked.disks.virtio | default({}) | combine(install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'zfs') | map(attribute='value.backend') | map('default', 'default') | unique | list }}" + delegate_to: "{{ vm_host_cooked.name }}" zfs: name: "{{ vm_host_cooked.zfs[item].pool }}/{{ vm_host_cooked.zfs[item].name }}/{{ install_hostname }}" state: present @@ -19,72 +21,75 @@ mountpoint: none - name: create zfs-based disk volumes for vm - loop: "{{ hostvars[install_hostname].install_cooked.disks.virtio | default({}) | combine(hostvars[install_hostname].install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'zfs') | list }}" + loop: "{{ install_cooked.disks.virtio | default({}) | combine(install_cooked.disks.scsi | default({})) | dict2items | selectattr('value.type', 'eq', 'zfs') | list }}" loop_control: label: "{{ item.value.name }} on backend {{ item.value.backend | default('default') }} ({{ item.value.size }})" + delegate_to: "{{ vm_host_cooked.name }}" zfs: name: "{{ vm_host_cooked.zfs[item.value.backend | default('default')].pool }}/{{ vm_host_cooked.zfs[item.value.backend | default('default')].name }}/{{ install_hostname }}/{{ item.value.name }}" state: present extra_zfs_properties: "{{ item.value.properties | default({}) | combine({'volsize': item.value.size}) }}" -- block: - - name: create a temporary workdir - tempfile: - path: "{{ installer_base_path }}/" - prefix: ".{{ install_hostname }}." - state: directory - register: tmpdir +- delegate_to: "{{ vm_host_cooked.name }}" + block: + - name: create a temporary workdir + tempfile: + path: "{{ hostvars[vm_host_cooked.name].installer_base_path }}/" + prefix: ".{{ install_hostname }}." + state: directory + register: tmpdir - - when: install_distro in ['debian', 'ubuntu'] - import_tasks: installer-debian.yml + - when: install_distro in ['debian', 'ubuntu'] + import_tasks: installer-debian.yml - - when: install_distro in ['openbsd'] - import_tasks: installer-openbsd.yml + - when: install_distro in ['openbsd'] + import_tasks: installer-openbsd.yml - - name: Make installer workdir readable by qemu - acl: - path: "{{ tmpdir.path }}" - state: present - entity: libvirt-qemu - etype: user - permissions: rx + - name: Make installer workdir readable by qemu + acl: + path: "{{ tmpdir.path }}" + state: present + entity: libvirt-qemu + etype: user + permissions: rx - - name: define installer vm - vars: - vm_define_installer: yes - installer_tmpdir: "{{ tmpdir.path }}" - import_role: - name: vm/guest/define + - name: define installer vm + vars: + vm_define_installer: yes + installer_tmpdir: "{{ tmpdir.path }}" + import_role: + name: vm/guest/define - - debug: - msg: "you can check on the status of the installer running this command 'virsh console {{ install_hostname }}' on host {{ inventory_hostname }}." + - debug: + msg: "you can check on the status of the installer running this command 'virsh console {{ install_hostname }}' on host {{ vm_host_cooked.name }}." - - when: installer_manual_steps_msg is defined - pause: - prompt: | - Mind that this installer needs manual steps to be performed: + - when: installer_manual_steps_msg is defined + pause: + prompt: | + Mind that this installer needs manual steps to be performed: - {{ installer_manual_steps_msg | indent(2) }} + {{ installer_manual_steps_msg | indent(2) }} - When done press ENTER to continue or CTRL-C then A to abort. + When done press ENTER to continue or CTRL-C then A to abort. - - name: wait for installer to finish or crash - wait_for_virt: - name: "{{ install_hostname }}" - states: shutdown,crashed - timeout: 1800 - register: installer_result - failed_when: installer_result.failed or installer_result.state == "crashed" + - name: wait for installer to finish or crash + wait_for_virt: + name: "{{ install_hostname }}" + states: shutdown,crashed + timeout: 1800 + register: installer_result + failed_when: installer_result.failed or installer_result.state == "crashed" always: - - name: cleanup temporary workdir - file: - path: "{{ tmpdir.path }}" - state: absent + - name: cleanup temporary workdir + file: + path: "{{ tmpdir.path }}" + state: absent - name: define vm vars: vm_define_installer: no + delegate_to: "{{ vm_host_cooked.name }}" import_role: name: vm/guest/define |