Terraform provider for Libvirt

Instal mkisofs for building the cloud-init ISO file(s).

apt-get install -y mkisofs

Install Terraform

cd /usr/src

wget https://releases.hashicorp.com/terraform/0.11.3/terraform_0.11.3_linux_amd64.zip

unzip -d /usr/bin/ terraform_0.11.3_linux_amd64.zip

chmod 0755 /usr/bin/terraform

Install Terraform Libvirt Provider

  • https://github.com/dmacvicar/terraform-provider-libvirt
cd /usr/src

wget https://github.com/dmacvicar/terraform-provider-libvirt/releases/download/v0.5.1/terraform-provider-libvirt-0.5.1.Ubuntu_18.04.amd64.tar.gz

mkdir -p ~/.terraform.d/plugins

tar zxf terraform-provider-libvirt-0.5.1.Ubuntu_18.04.amd64.tar.gz -C ~/.terraform.d/plugins/

chmod 0755 ~/.terraform.d/plugins/terraform-provider-libvirt

Create a terraform file

FILE: userdata.yaml

#cloud-config
ssh_pwauth: True
groups:
  - ubuntu: [root,sys]
users:
  - name:          ubuntu
    gecos:         ubuntu
    primary_group: ubuntu
    groups:        [users,sudo,ssh,staff]
    expiredate:    "2099-09-09"
    lock_passwd:   false
    passwd:        "!"
    sudo:          "ALL=(ALL) NOPASSWD:ALL"
    ssh_authorized_keys:
      - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdeaJgkpFXx14PeGeyTGP16SF/ZkL0CFZjH2XISToS8KMXg9q1L1B+ehIsOshK7uBJsb9JvAkIBxr/yliMsPdxtnKPiOa444ZN/cS8JmA9k2KxEvXBwBlxh13RAN1wM1mqZrQLnoOITHUj//8oTKauQOu1qtIpmQ9vw91c4ZXEBXDoeuRHkNsq3LlGpO12psX3rMUPuWOWXdAksfob/lpK88kKU45ymPvoKPkouSJeC0v0S5OnH3NJvhgDS1ercBe/BCg4i1NlCTNeVfj9DRWLcbGUQzTbzxLk8hAF7EXD3dlGGLM1ogeSfOYPJ4EPCLRmA3WoOIHalHhPpq3atQv1 devon@kvm1"

FILE: netconfig.yaml

network:
version: 2
        ethernets:
         eno1:
         dhcp4: true

FILE: libvirt.tf

provider "libvirt" {
  uri = "qemu:///system"
}

resource "libvirt_volume" "ubuntu_server_1804" {
  name   = "ubuntu-server-1804.img"
  pool   = "default"
  source = "https://cloud-images.ubuntu.com/releases/18.04/release/ubuntu-18.04-server-cloudimg-amd64.img"
  format = "qcow2"
}

resource "libvirt_network" "vm_network" {
 name = "vm_network"
 addresses = ["192.168.124.0/24"]
}



data "template_file" "user_data" {
  template = "${file("${path.module}/userdata.yaml")}"
}

data "template_file" "network_config" {
  template = "${file("${path.module}/netconfig.yaml")}"
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name           = "commoninit.iso"
  user_data      = "${data.template_file.user_data.rendered}"
  network_config = "${data.template_file.network_config.rendered}"
}


resource "libvirt_domain" "ubn-srv-001" {
  name   = "ubn-srv-001"
  memory = "512"
  vcpu   = 1

  cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"

  network_interface {
    hostname     = "master"
    network_name = "vm_network"
  }

  # Ubuntu can hang if a serial console is not present at boot time.
  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  disk {
    volume_id = "${libvirt_volume.ubuntu_server_1804.id}"
  }
  graphics {
    type        = "spice"
    listen_type = "address"
    autoport    = "true"
  }
}

##
## Print the IP
## Can use `virsh domifaddr <vm_name> <interface>` to get the ip later
##
output "ip" {
  value = "${libvirt_domain.ubn-srv-001.network_interface.0.addresses.0}"
}

Verify the terraform template

terraform validate

Plan the changes

terraform plan

Apply the changes

terraform apply

Troubleshooting

provider.libvirt: new or changed plugin executable

  • You need to Initialize the provider plugins.
  • In an empty directory, create a file containing:
provider "libvirt" {
  uri = "qemu:///system"
}

data "template_file" "network_config" {
  template = "${file("${path.module}/netconfig.yaml")}"
}
  • Run terraform init to initialize the plugins.
terraform init
categories: kvm | terraform | libvirt |