From 63fd5d224bdb48550754aa803ab24858e34add85 Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Mon, 25 Mar 2024 18:32:11 +0100 Subject: [PATCH] Libvirt: enable running parameter for domain This commit enables the "running" (true by default) parameter for the domain that will define whether or not the domain should be started. NOTE: this will omit from the network configuration, otherwise the VM will always be turned up. --- templates/libvirt/vms.tf.j2 | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/templates/libvirt/vms.tf.j2 b/templates/libvirt/vms.tf.j2 index 60b54f4..5c85e1b 100644 --- a/templates/libvirt/vms.tf.j2 +++ b/templates/libvirt/vms.tf.j2 @@ -25,6 +25,7 @@ resource "libvirt_volume" "{{ disk.volume_id }}" { # VM {{ vm.name }} specification resource "libvirt_domain" "{{ vm.name }}" { name = "{{ vm.name }}" + running = "{{ vm.running | default('true') | lower() }}" memory = "{{ vm.memory }}" vcpu = "{{ vm.vcpu }}" @@ -37,21 +38,6 @@ resource "libvirt_domain" "{{ vm.name }}" { cloudinit = libvirt_cloudinit_disk.{{ vm.cloudinit_disk }}.id {% endif %} -{% for interface in vm.network_interfaces %} - network_interface { - network_id = libvirt_network.{{ interface.network_id }}.id -{% if interface.addresses is defined %} - addresses = [{% for address in interface.addresses %}"{{ address }}"{%- if not loop.last -%}, {%- endif -%}{% endfor %}] -{% endif %} -{% if interface.mac is defined %} - mac = "{{ interface.mac }}" -{% endif %} -{% if interface.wait_for_lease is defined %} - wait_for_lease = {{ interface.wait_for_lease }} -{% endif %} - } -{% endfor %} - {% for disk in vm.disks %} disk { volume_id = libvirt_volume.{{ disk.volume_id }}.id @@ -79,5 +65,22 @@ resource "libvirt_domain" "{{ vm.name }}" { } {% endfor %} {% endif %} + +{% if vm.running | default('true') | bool() %} +{% for interface in vm.network_interfaces %} + network_interface { + network_id = libvirt_network.{{ interface.network_id }}.id +{% if interface.addresses is defined %} + addresses = [{% for address in interface.addresses %}"{{ address }}"{%- if not loop.last -%}, {%- endif -%}{% endfor %}] +{% endif %} +{% if interface.mac is defined %} + mac = "{{ interface.mac }}" +{% endif %} +{% if interface.wait_for_lease is defined %} + wait_for_lease = {{ interface.wait_for_lease }} +{% endif %} + } +{% endfor %} +{% endif %} } {% endfor %}