Ansible-Bootstrap/roles/virtualization/tasks/libvirt.yml

44 lines
1.5 KiB
YAML
Raw Permalink Normal View History

2024-07-11 22:20:45 +02:00
---
2024-03-19 23:02:50 +01:00
- name: Check if VM disk exists
delegate_to: localhost
2024-07-11 22:20:45 +02:00
ansible.builtin.stat:
path: "{{ vm_path | default('/var/lib/libvirt/images/') }}{{ hostname }}.qcow2"
2024-03-19 23:02:50 +01:00
register: vm_disk_stat
- name: Create VM disk
when: not vm_disk_stat.stat.exists
delegate_to: localhost
2024-07-11 22:20:45 +02:00
ansible.builtin.command: qemu-img create -f qcow2 {{ vm_path | default('/var/lib/libvirt/images/') }}{{ hostname }}.qcow2 {{ vm_size }}G
2024-03-19 23:02:50 +01:00
- name: Generate Random MAC Address
delegate_to: localhost
2024-07-11 22:20:45 +02:00
ansible.builtin.shell: openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | sed 's/^/02:/'
2024-03-19 23:02:50 +01:00
changed_when: false
register: mac_address_output
- name: Render cloud config templates
delegate_to: localhost
2024-07-11 22:20:45 +02:00
ansible.builtin.template:
2024-03-19 23:02:50 +01:00
src: "{{ item.src }}"
2024-07-11 22:20:45 +02:00
dest: /tmp/{{ item.dest_prefix }}-{{ hostname }}.yml
2024-03-19 23:02:50 +01:00
loop:
2024-07-11 22:20:45 +02:00
- { src: cloud-user-data.yml.j2, dest_prefix: cloud-user-data }
- { src: cloud-network-config.yml.j2, dest_prefix: cloud-network-config }
2024-03-19 23:02:50 +01:00
- name: Create cloud-init disk
delegate_to: localhost
2024-07-11 22:20:45 +02:00
ansible.builtin.command: cloud-localds {{ vm_path | default('/var/lib/libvirt/images/') }}{{ hostname }}-cloudinit.iso /tmp/cloud-user-data-{{ hostname }}.yml -N
/tmp/cloud-network-config-{{ hostname }}.yml
2024-03-19 23:02:50 +01:00
- name: Create VM using libvirt
delegate_to: localhost
community.libvirt.virt:
command: define
xml: "{{ lookup('template', 'vm.xml.j2') }}"
2024-07-11 22:20:45 +02:00
- name: Start vm
2024-03-19 23:02:50 +01:00
delegate_to: localhost
community.libvirt.virt:
name: "{{ hostname }}"
2024-07-11 22:20:45 +02:00
state: running