2022-10-24 10:54:04 +05:30
terraform {
required_version = " >= 0.13 "
required_providers {
libvirt = {
source = " dmacvicar/libvirt "
version = " ~> 0.7.0 "
}
}
}
# instance the provider
provider " libvirt " {
uri = " qemu:///system "
}
resource " libvirt_pool " " ubuntu " {
name = " ubuntu "
type = " dir "
path = " /tmp/terraform-provider-libvirt-pool-ubuntu "
}
# We fetch the latest ubuntu release image from their mirrors
resource " libvirt_volume " " ubuntu-qcow2 " {
name = " ubuntu-qcow2 "
pool = libvirt_pool . ubuntu . name
source = " https://cloud-images.ubuntu.com/releases/xenial/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img "
format = " qcow2 "
}
2022-10-24 11:27:14 +05:30
# We fetch the latest ubuntu release image from their mirrors
resource " libvirt_volume " " debian-qcow2 " {
name = " debian-qcow2 "
pool = libvirt_pool . ubuntu . name
source = " https://cloud.debian.org/images/cloud/buster/latest/debian-10-genericcloud-amd64.qcow2 "
format = " qcow2 "
}
2022-10-24 10:54:04 +05:30
data " template_file " " user_data " {
template = file ( " ${ path . module } /cloud_init.cfg " )
}
data " template_file " " network_config " {
template = file ( " ${ path . module } /network_config.cfg " )
}
# for more info about paramater check this out
# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/cloudinit.html.markdown
# Use CloudInit to add our ssh-key to the instance
# you can add also meta_data field
resource " libvirt_cloudinit_disk " " commoninit " {
name = " commoninit.iso "
user_data = data . template_file . user_data . rendered
network_config = data . template_file . network_config . rendered
pool = libvirt_pool . ubuntu . name
}
# Create the machine
resource " libvirt_domain " " domain-ubuntu " {
name = " ubuntu-terraform "
memory = " 512 "
vcpu = 1
cloudinit = libvirt_cloudinit_disk . commoninit . id
network_interface {
network_name = " default "
2022-10-24 15:37:23 +05:30
wait_for_lease = true
2022-10-24 10:54:04 +05:30
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = " pty "
target_port = " 0 "
target_type = " serial "
}
console {
type = " pty "
target_type = " virtio "
target_port = " 1 "
}
disk {
volume_id = libvirt_volume . ubuntu - qcow2 . id
}
graphics {
type = " spice "
listen_type = " address "
autoport = true
}
}
2022-10-24 11:27:14 +05:30
# Create the machine
resource " libvirt_domain " " domain-debian-ansible-playground " {
name = " debian-ansible-playground-terraform "
memory = " 3000 "
vcpu = 4
cloudinit = libvirt_cloudinit_disk . commoninit . id
network_interface {
network_name = " default "
2022-10-24 15:37:23 +05:30
wait_for_lease = true
2022-10-24 11:27:14 +05:30
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = " pty "
target_port = " 0 "
target_type = " serial "
}
console {
type = " pty "
target_type = " virtio "
target_port = " 1 "
}
disk {
volume_id = libvirt_volume . debian - qcow2 . id
}
graphics {
type = " spice "
listen_type = " address "
autoport = true
}
}
2022-10-24 10:54:04 +05:30
# IPs: use wait_for_lease true or after creation use terraform refresh and terraform show for the ips of domain
2022-10-24 15:37:23 +05:30
output " ubuntu_ip " {
value = length ( libvirt_domain . domain - ubuntu . network_interface [ 0 ] . addresses ) > 0 ? libvirt_domain . domain - ubuntu . network_interface [ 0 ] . addresses [ 0 ] : " "
}
output " debian_ip " {
value = length ( libvirt_domain . domain - debian - ansible - playground . network_interface [ 0 ] . addresses ) > 0 ? libvirt_domain . domain - debian - ansible - playground . network_interface [ 0 ] . addresses [ 0 ] : " "
}