first draft

This commit is contained in:
2021-06-06 21:46:18 +02:00
commit 09d4930cd2
14 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
---
# VM PREPARATION
- name: create vm storage
delegate_to: "{{ virt.host }}"
copy:
src: "{{ libvirt.images }}/{{ virt.image }}"
dest: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"
force: no
- name: set vm storage size
delegate_to: "{{ virt.host }}"
shell:
cmd: qemu-img resize "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2" "{{ virt.hdd }}"
- name: define vm
delegate_to: "{{ virt.host }}"
virt:
name: "{{ inventory_hostname }}"
xml: '{{ lookup("template", "fedora-34.xml.j2") }}'
command: define
- name: make temp dir for cloud-init (CI)
delegate_to: "{{ virt.host }}"
tempfile:
state: directory
register: cidata
# CLOUD-INIT PREPARATION
- name: create CI-metadata
delegate_to: "{{ virt.host }}"
template:
src: ci-metadata.j2
dest: "{{ cidata.path }}/meta-data"
- name: create CI-userdata
delegate_to: "{{ virt.host }}"
template:
src: ci-userdata.j2
dest: "{{ cidata.path }}/user-data"
- name: create CI ISO
delegate_to: "{{ virt.host }}"
shell:
cmd: genisoimage -output "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" -volid cidata -joliet -rock user-data meta-data
chdir: "{{ cidata.path }}"
# VM INIT
- name: attach CI to vm
delegate_to: "{{ virt.host }}"
shell:
cmd: virsh attach-disk "{{ inventory_hostname }}" --source "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" --target sda --type cdrom --mode readonly --persistent
# CLOUD-INIT CLEANUP
- name: delete CI temp dir
delegate_to: "{{ virt.host }}"
file:
path: "{{ cidata.path }}"
state: absent
force: true
- name: delete CI ISO
delegate_to: "{{ virt.host }}"
file:
path: "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso"
state: absent

View File

@@ -0,0 +1,13 @@
---
- name: check vm existance
delegate_to: "{{ virt.host }}"
stat:
path: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"
get_checksum: false
get_attributes: false
get_mime: false
register: vmimg
- name: create vm
include_tasks: create_vm.yml
when: not vmimg.stat.exists