From 42650055c6066989bb2c0d38613c6028b6188e9d Mon Sep 17 00:00:00 2001 From: binary Date: Tue, 19 Jan 2021 19:13:27 +0100 Subject: Refactor bhyve role for new network setup --- roles/bhyve/tasks/guest.yml | 4 ++-- roles/bhyve/tasks/main.yml | 42 ++++++++++++++++++++++++++++-------------- roles/bhyve/vars/main.yml | 1 + 3 files changed, 31 insertions(+), 16 deletions(-) (limited to 'roles/bhyve') diff --git a/roles/bhyve/tasks/guest.yml b/roles/bhyve/tasks/guest.yml index 9cabf90..b24b434 100644 --- a/roles/bhyve/tasks/guest.yml +++ b/roles/bhyve/tasks/guest.yml @@ -4,11 +4,11 @@ --- -- name: check kvm directory +- name: check vm directory stat: path="/data/bhyve/{{ guest.name }}" register: st -- name: create all vm bound with current host +- name: create all disks bound with current host shell: vm create -s "{{ guest.disks[0].size }}" "{{ guest.name }}" when: st.stat.isdir is not defined diff --git a/roles/bhyve/tasks/main.yml b/roles/bhyve/tasks/main.yml index 3ff902a..039ba10 100644 --- a/roles/bhyve/tasks/main.yml +++ b/roles/bhyve/tasks/main.yml @@ -1,6 +1,6 @@ # bhyve ~~ roles/bhyve/tasks/main.yml -# bhyve hypervisor configuration +# bhyve hypervisor configuration for FreeBSD based systems --- @@ -25,8 +25,9 @@ mode: 0711 - name: load bhyve module - shell: "! kldstat | grep vmm && kldload vmm" - failed_when: false + shell: kldload vmm + register: result + failed_when: result.rc != 0 and "already loaded" not in result.stderr - name: install bhyve components package: name="{{ item }}" @@ -36,10 +37,15 @@ - grub2-bhyve - devcpu-data +- name: retrieve all valid vms + set_fact: valid_vms="{% for vm in vms %}{{ vm.name }} {% endfor %}" + - name: enable bhyve vm shell: | sysrc vm_enable="YES" - sysrc vm_dir="/data/bhyve" + sysrc vm_dir="{{ bhyve_dir }}" + sysrc vm_list="{{ valid_vms }}" + sysrc vm_delay="{{ bhyve_bootdelay }}" - name: init vm directory and filesets shell: vm init @@ -49,19 +55,27 @@ 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 - name: create per vm configuration include: guest.yml guest="{{ item }}" loop: "{{ vms }}" -- name: activate on boot all enabled vm - lineinfile: - path: /etc/rc.conf - regexp: "^vm_list=" - line: vm_list="{% for vm in vms %}{{ vm.name }} {% endfor %}" +- name: retrieve all deprecated vms + find: + path: "{{ bhyve_dir }}" + file_type: directory + register: deprecated_vms + +- name: delete all deprecated vms + file: + path: "{{ item[1].path }}" + state: absent + when: (item[1].path | basename) not in "{{ valid_vms }}" + with_nested: + - "{{ vms }}" + - "{{ deprecated_vms.files }}" -- name: set delay on boot between vm - lineinfile: - path: /etc/rc.conf - regexp: "^vm_delay=" - line: vm_delay="5" +- name: ensure all vms are started + shell: vm startall diff --git a/roles/bhyve/vars/main.yml b/roles/bhyve/vars/main.yml index 7819620..51813f5 100644 --- a/roles/bhyve/vars/main.yml +++ b/roles/bhyve/vars/main.yml @@ -7,6 +7,7 @@ bhyve_dir: "/data/bhyve" bhyve_user: "bhyve" bhyve_group: "bhyve" +bhyve_bootdelay: "3" bhyve_iso: -- cgit v1.2.3