2022-01-05 23:59:13 +01:00
|
|
|
---
|
|
|
|
- name: Provision VM
|
|
|
|
block:
|
|
|
|
- name: Query volumes
|
|
|
|
ansible.builtin.command:
|
|
|
|
argv:
|
|
|
|
- /usr/bin/virsh
|
|
|
|
- -c
|
|
|
|
- "{{ libvirt_url }}"
|
|
|
|
- vol-list
|
|
|
|
- "{{ storage.name }}"
|
|
|
|
register: volumes
|
|
|
|
no_log: true
|
|
|
|
|
2022-02-13 01:37:39 +01:00
|
|
|
- name: Create storage template
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: "../templates/libvirt-storage-template.xml.j2"
|
|
|
|
dest: "../templates/generated/libvirt-storage-{{ inventory_hostname }}.xml"
|
|
|
|
group: lysergic
|
|
|
|
mode: '0660'
|
|
|
|
when: vm_name not in volumes.stdout
|
|
|
|
|
2022-01-05 23:59:13 +01:00
|
|
|
- name: Define volume
|
|
|
|
ansible.builtin.command:
|
|
|
|
argv:
|
|
|
|
- /usr/bin/virsh
|
|
|
|
- -c
|
|
|
|
- "{{ libvirt_url }}"
|
|
|
|
- vol-create
|
|
|
|
- "{{ storage.name }}"
|
|
|
|
- "../templates/generated/libvirt-storage-{{ inventory_hostname }}.xml"
|
|
|
|
when: vm_name not in volumes.stdout
|
|
|
|
|
2022-02-13 01:37:39 +01:00
|
|
|
# https://gitlab.com/libvirt/libvirt/-/issues/135
|
|
|
|
- name: Fetch volume path
|
|
|
|
ansible.builtin.command:
|
|
|
|
argv:
|
|
|
|
- /usr/bin/virsh
|
|
|
|
- -c
|
|
|
|
- "{{ libvirt_url }}"
|
|
|
|
- vol-path
|
|
|
|
- --pool
|
|
|
|
- "{{ storage.name }}"
|
|
|
|
- "{{ inventory_hostname }}_root_disk.qcow2"
|
|
|
|
register: volpath
|
|
|
|
|
|
|
|
- name: Store volume path
|
|
|
|
set_fact:
|
|
|
|
volume_path: "{{ volpath.stdout }}"
|
|
|
|
|
|
|
|
- name: Create domain template
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: "../templates/libvirt-template.xml.j2"
|
2022-02-13 16:56:12 +01:00
|
|
|
dest: "../templates/generated/libvirt-{{ inventory_hostname }}.xml"
|
2022-02-13 01:37:39 +01:00
|
|
|
group: lysergic
|
|
|
|
mode: '0660'
|
|
|
|
|
|
|
|
- name: Define domain
|
|
|
|
community.libvirt.virt:
|
|
|
|
uri: "{{ libvirt_url }}"
|
|
|
|
command: define
|
|
|
|
xml: "{{ lookup('template', '../templates/libvirt-template.xml.j2') }}"
|
|
|
|
autostart: no
|
|
|
|
# delegate_to: localhost
|
|
|
|
|
2022-01-05 23:59:13 +01:00
|
|
|
- name: Fetch MAC address
|
2022-02-13 01:37:39 +01:00
|
|
|
ansible.builtin.shell: "/usr/bin/virsh -c {{ libvirt_url }} domiflist {{ vm_name }} | awk '{print $5}' | cut -d/ -f 1 | tail -n 2 | head -n 1" # ewww :-(
|
2022-01-05 23:59:13 +01:00
|
|
|
register: domiflist_mac
|
|
|
|
|
|
|
|
- name: Store MAC address
|
|
|
|
set_fact:
|
|
|
|
mac_address: "{{ domiflist_mac.stdout }}"
|
|
|
|
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
always:
|
|
|
|
- name: Debug
|
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ libvirt_url if libvirt_url is defined }} - {{ storage.name if storage is defined }} - {{ mac_address if mac_address is defined }}"
|