41 lines
1.4 KiB
YAML
41 lines
1.4 KiB
YAML
|
- name: Check if VM disk exists
|
||
|
delegate_to: localhost
|
||
|
stat:
|
||
|
path: "{{ storage_path | default('/var/lib/libvirt/images/') }}{{ hostname }}.qcow2"
|
||
|
register: vm_disk_stat
|
||
|
|
||
|
- name: Create VM disk
|
||
|
when: not vm_disk_stat.stat.exists
|
||
|
delegate_to: localhost
|
||
|
command: "qemu-img create -f qcow2 {{ storage_path | default('/var/lib/libvirt/images/') }}{{ hostname }}.qcow2 {{ vm_size }}G"
|
||
|
|
||
|
- name: Generate Random MAC Address
|
||
|
delegate_to: localhost
|
||
|
shell: openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | sed 's/^/02:/'
|
||
|
changed_when: false
|
||
|
register: mac_address_output
|
||
|
|
||
|
- name: Render cloud config templates
|
||
|
delegate_to: localhost
|
||
|
template:
|
||
|
src: "{{ item.src }}"
|
||
|
dest: "/tmp/{{ item.dest_prefix }}-{{ hostname }}.yml"
|
||
|
loop:
|
||
|
- { src: "cloud-user-data.yml.j2", dest_prefix: "cloud-user-data" }
|
||
|
- { src: "cloud-network-config.yml.j2", dest_prefix: "cloud-network-config" }
|
||
|
|
||
|
- name: Create cloud-init disk
|
||
|
delegate_to: localhost
|
||
|
command: "cloud-localds {{ storage_path | default('/var/lib/libvirt/images/') }}{{ hostname }}-cloudinit.iso /tmp/cloud-user-data-{{ hostname }}.yml -N /tmp/cloud-network-config-{{ hostname }}.yml"
|
||
|
|
||
|
- name: Create VM using libvirt
|
||
|
delegate_to: localhost
|
||
|
community.libvirt.virt:
|
||
|
command: define
|
||
|
xml: "{{ lookup('template', 'vm.xml.j2') }}"
|
||
|
|
||
|
- name: start vm
|
||
|
delegate_to: localhost
|
||
|
community.libvirt.virt:
|
||
|
name: "{{ hostname }}"
|
||
|
state: running
|