diff --git a/ansible/roles/hypervisor_qcow/tasks/libvirt_dir.yaml b/ansible/roles/hypervisor_qcow/tasks/libvirt_dir.yaml index 8665fff..c9774f8 100644 --- a/ansible/roles/hypervisor_qcow/tasks/libvirt_dir.yaml +++ b/ansible/roles/hypervisor_qcow/tasks/libvirt_dir.yaml @@ -1,8 +1,61 @@ --- - name: Format and mount the libvirt disk if it is not root - when: hypervisor.device not in (ansible_mounts | json_query('[?mount == `/`].device')) + when: + - hypervisor.device not in (ansible_mounts | json_query('[?mount == `/`].device')) + - hypervisor.device not in (ansible_mounts | json_query('[?mount == `/var/lib/libvirt`].device')) ansible.builtin.include_tasks: file: libvirt_mount.yaml - - \ No newline at end of file + +- name: Create the libvirt storage directories + ansible.builtin.file: + path: "{{ item }}" + state: directory + owner: libvirt-qemu + group: libvirt-qemu + mode: '0775' + loop: + - /var/lib/libvirt/vhds/ + - /var/lib/libvirt/isos/ + - /var/lib/libvirt/nvram/ + +- name: Remove the default libvirt storage pool + community.libvirt.virt_pool: + name: default + state: deleted + +- name: Get libvirt storage pool facts + community.libvirt.virt_pool: + command: facts + +- name: Define the libvirt storage pools + community.libvirt.virt_pool: + name: "{{ item.name }}" + command: define + xml: "{{ lookup('template', 'dir_pool.xml.j2') }}" + loop: + - name: vhds + path: /var/lib/libvirt/vhds/ + - name: isos + path: /var/lib/libvirt/isos/ + - name: nvram + path: /var/lib/libvirt/nvram/ + +- name: Create the libvirt storage pools + community.libvirt.virt_pool: + name: "{{ item }}" + command: build + loop: + - vhds + - isos + - nvram + +- name: Start the libvirt storage pools + community.libvirt.virt_pool: + name: "{{ item }}" + state: active + autostart: true + loop: + - vhds + - isos + - nvram diff --git a/ansible/roles/hypervisor_qcow/tasks/libvirt_mount.yaml b/ansible/roles/hypervisor_qcow/tasks/libvirt_mount.yaml new file mode 100644 index 0000000..84feaf7 --- /dev/null +++ b/ansible/roles/hypervisor_qcow/tasks/libvirt_mount.yaml @@ -0,0 +1,74 @@ +--- + +- name: Configure disk partition + community.general.parted: + align: optimal + device: "{{ hypervisor.device }}" + fs_type: ext4 + label: gpt + name: libvirt + number: 1 + part_end: 100% + part_start: 0% + state: present + +- name: Format filesystem + community.general.filesystem: + device: "{{ hypervisor.device }}1" + fstype: ext4 + resizefs: true + state: present + +- name: Stop the libvirt service + ansible.builtin.service: + name: libvirtd + state: stopped + +- name: Temp mount and copy block + block: + + - name: Temporarily mount hypervisor storage + ansible.posix.mount: + path: /mnt/libvirt_temp/ + src: "{{ hypervisor.device }}1" + fstype: ext4 + state: mounted + boot: false + + - name: Copy libvirt contents to hypervisor storage + ansible.builtin.copy: + src: /var/lib/libvirt/ + dest: /mnt/libvirt_temp/ + remote_src: true + mode: preserve + + always: + + - name: Unmount from temporary mount point + ansible.posix.mount: + path: /mnt/libvirt_temp/ + state: absent + +- name: Remove existing libvirt storage + ansible.builtin.file: + path: /var/lib/libvirt/ + state: "{{ item }}" + owner: root + group: root + mode: '0775' + loop: + - absent + - directory + +- name: Mount hypervisor storage + ansible.posix.mount: + path: /var/lib/libvirt/ + src: "{{ hypervisor.device }}1" + fstype: ext4 + state: mounted + boot: true + +- name: Start the libvirt service + ansible.builtin.service: + name: libvirtd + state: started diff --git a/ansible/roles/hypervisor_qcow/tasks/main.yaml b/ansible/roles/hypervisor_qcow/tasks/main.yaml index b6ff939..90c3b9a 100644 --- a/ansible/roles/hypervisor_qcow/tasks/main.yaml +++ b/ansible/roles/hypervisor_qcow/tasks/main.yaml @@ -13,6 +13,16 @@ groups: libvirt append: true +- name: Set required sysctl flags for bridging + ansible.posix.sysctl: + name: "{{ item.name }}" + value: "{{ item.value }}}}" + state: present + sysctl_set: true + reload: true # TODO set sysctl file? + loop: + - net.ipv4.ip_forward # TODO add remaining values here + - name: Add bridge(s) to qemu_bridge_helper when: qemu_bridges is defined ansible.builtin.lineinfile: @@ -39,7 +49,7 @@ name: default state: absent -- name: Setup libvirt storage (qcow) +- name: Setup libvirt storage (dir) when: hypervisor.storage == 'dir' ansible.builtin.include_tasks: file: libvirt_dir.yaml diff --git a/ansible/roles/hypervisor_qcow/templates/dir_libvirt_pool.xml.j2 b/ansible/roles/hypervisor_qcow/templates/dir_libvirt_pool.xml.j2 new file mode 100644 index 0000000..7391245 --- /dev/null +++ b/ansible/roles/hypervisor_qcow/templates/dir_libvirt_pool.xml.j2 @@ -0,0 +1,6 @@ + + {{ item.name }} + + {{ item.path }} + + \ No newline at end of file diff --git a/ansible/roles/hypervisor_qcow/templates/zfs_libvirt_pool.xml.j2 b/ansible/roles/hypervisor_qcow/templates/zfs_libvirt_pool.xml.j2 new file mode 100644 index 0000000..d1cdfef --- /dev/null +++ b/ansible/roles/hypervisor_qcow/templates/zfs_libvirt_pool.xml.j2 @@ -0,0 +1,6 @@ + + {{ item.name }} + + {{ item.dataset }} + + \ No newline at end of file