vm-setup/roles/create-vm/tasks/main.yml
2022-06-16 20:43:06 +02:00

73 lines
2 KiB
YAML

---
- name: check if vm name exists in hostvars
local_action: stat path="host_vars/{{ vmname }}"
register: register_name
- name: Stop if host_vars exist
debug:
msg: "The file or directory exists"
failed_when: register_name.stat.exists
when:
- register_name.stat.exists
- not recreate
- name: add new vm to hostvars
include_tasks: create_hostvars.yml
when: not recreate
- name: install libvirt and co
package:
name:
- libvirt-daemon-system
- qemu-system
- virtinst
- qemu-utils
- libvirt-clients
- genisoimage
state: latest
become: true
- name: Create images directory
file:
path: '{{ images_dir }}'
state: directory
owner: libvirt-qemu
group: libvirt-qemu
become: true
- name: check for cloudimage
find:
age: -26w
path: '{{ images_dir }}'
pattern: '{{ base_image }}'
register: recent_cloudimage
- debug:
msg: "{{ recent_cloudimage.matched }}"
- name: download cloud image template, if none found or to old
get_url:
url: '{{ image_url }}'
dest: '{{ images_dir }}'
checksum: 'sha512:{{ image_checksum }}'
when: not recent_cloudimage.matched
- name: Create VM image from base image
command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}{{ vmname }}.img {{ image_capacity }}
- name: Create user-data
template:
src: user-data.j2
dest: '{{ images_dir }}/user-data'
- name: Create meta-data
template:
src: meta-data.j2
dest: '{{ images_dir }}/meta-data'
- name: Create cloud-init configuration image
command: genisoimage -output {{ images_dir }}/{{ vmname }}-cidata.iso -V cidata -r -J {{ images_dir }}/user-data {{ images_dir }}/meta-data
become: true
- name: Create the VM
command: virt-install --name={{ vmname }} --ram={{ ram }} --vcpus={{ vcpus }} --import --disk path={{ images_dir }}{{ vmname }}.img,format=qcow2 --disk path={{ images_dir }}{{ vmname }}-cidata.iso,device=cdrom --os-variant {{ os }} --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole