ansible-test/roles/virt_vm/tasks/create_vm.yml
2021-06-06 21:46:18 +02:00

72 lines
1.7 KiB
YAML

---
# 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