From 173ed411dc8b7feed16abd59cead081b209a66fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Tue, 4 May 2021 22:21:17 +0200 Subject: roles/bhyve: Use computed variables for vms --- roles/bhyve/defaults/main.yml | 8 ++++- roles/bhyve/tasks/main.yml | 68 +++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/roles/bhyve/defaults/main.yml b/roles/bhyve/defaults/main.yml index c237cac..ef9efc7 100644 --- a/roles/bhyve/defaults/main.yml +++ b/roles/bhyve/defaults/main.yml @@ -12,4 +12,10 @@ bhyve_strict: true bhyve_iso: - https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/x86_64/alpine-virt-3.13.0-x86_64.iso - - https://cdn.openbsd.org/pub/OpenBSD/6.8/amd64/cd68.iso + - https://cdn.openbsd.org/pub/OpenBSD/6.9/amd64/cd69.iso + +bhyve_dependencies: + - vm-bhyve + - bhyve-firmware + - grub2-bhyve + - devcpu-data diff --git a/roles/bhyve/tasks/main.yml b/roles/bhyve/tasks/main.yml index 1b41de0..e39e5e7 100644 --- a/roles/bhyve/tasks/main.yml +++ b/roles/bhyve/tasks/main.yml @@ -4,8 +4,9 @@ --- -- name: retrieve all valid vms - set_fact: valid_vms="{% for vm in vms %}{{ vm.name }} {% endfor %}" +- name: retrieve active vms + set_fact: + active_vms: "{{ vms | map(attribute='name') | join(' ') }}" - name: create bhyve group group: @@ -14,49 +15,45 @@ - name: create bhyve user user: - name: bhyve - group: bhyve + name: "{{ bhyve_user }}" + group: "{{ bhyve_group }}" comment: Unpreviliged user for bhyve guests system: true - name: check existence of bhyve directory file: - path: /data/bhyve - owner: bhyve - group: bhyve + path: "{{ bhyve_dir }}" + owner: "{{ bhyve_user }}" + group: "{{ bhyve_user }}" state: directory mode: 0711 +- name: install bhyve dependencies + package: + name: "{{ bhyve_dependencies }}" + - name: load bhyve module shell: kldload vmm register: result failed_when: result.rc != 0 and "already loaded" not in result.stderr -- name: install bhyve components - package: name="{{ item }}" - loop: - - vm-bhyve - - bhyve-firmware - - grub2-bhyve - - devcpu-data - -- name: enable bhyve vm +- name: init bhyve vm shell: | sysrc vm_enable="YES" sysrc vm_dir="{{ bhyve_dir }}" - sysrc vm_list="{{ valid_vms }}" + sysrc vm_list="{{ active_vms }}" sysrc vm_delay="{{ bhyve_bootdelay }}" - -- name: init vm directory and filesets - shell: vm init + vm init - name: create network switch shell: | vm switch create {{ hypervisor.switch }} vm switch add public {{ hypervisor.interface }} - ignore_errors: true register: result - failed_when: result.rc != 0 and "already exists" not in result.stderr and "failed to" not in result.stderr + failed_when: > + result.rc != 0 and + "already exists" not in result.stderr and + "failed to" not in result.stderr - name: create per vm configuration include: guest.yml guest="{{ item }}" @@ -68,17 +65,30 @@ dest: "{{ bhyve_dir }}/.iso" loop: "{{ bhyve_iso }}" -- name: retrieve all active vms +- name: retrieve inactive vms find: path: "{{ bhyve_dir }}" file_type: directory - register: active_vms + register: result + +- name: compute retrieved vms to list + set_fact: + result: + "{{ result.files | map(attribute='path') | map('basename') | list }}" + active_vms: "{{ active_vms.split(' ') }}" + +- name: compute vms to be destroyed + set_fact: + inactive_vms: "{{ result | difference(active_vms) }}" + +- name: show vms to be destroyed + debug: + msg: "{{ inactive_vms }}" - name: delete all deprecated vms file: - path: "{{ item[1].path }}" + path: "{{ bhyve_dir }}/{{ item }}" state: absent - when: bhyve_strict and (item[1].path | basename) not in valid_vms - with_nested: - - "{{ vms }}" - - "{{ active_vms.files }}" + when: bhyve_strict + loop: "{{ inactive_vms }}" + -- cgit v1.2.3