aboutsummaryrefslogtreecommitdiffstats
path: root/roles/bhyve/tasks
diff options
context:
space:
mode:
authorRomain Gonçalves <me@rgoncalves.se>2021-05-04 22:21:17 +0200
committerRomain Gonçalves <me@rgoncalves.se>2021-05-04 22:21:17 +0200
commit173ed411dc8b7feed16abd59cead081b209a66fd (patch)
tree18d54e492a23994dabb31ed59b8eb4d583db3e77 /roles/bhyve/tasks
parent800a8e329f0717fd202cb4ec369be1438cab2729 (diff)
downloadinfrastructure-173ed411dc8b7feed16abd59cead081b209a66fd.tar.gz
roles/bhyve: Use computed variables for vms
Diffstat (limited to 'roles/bhyve/tasks')
-rw-r--r--roles/bhyve/tasks/main.yml68
1 files changed, 39 insertions, 29 deletions
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 }}"
+
remember that computers suck.