Terraform provider for libvirt
Using libvirt as a terraform provider
I’ve gotten comfortable with terraform using the openstack provider to build infrastructure. At home, I don’t have openstack, but I do have libvirt. The terraform-provider-libvirt plugin allows terraform to talk directly with libvirt to manage new resources. The main problem I faced with this setup is I don’t know go at all, so installing the plugin was a bit of a chore. The instructions below are the first thing that worked and might not be correct.
Terraform
Terraform is distributed as a single binary so installation is as simple as
downloading the package, unzipping
it, and copying the binary to (e.g.) /usr/local/bin/terraform
.
$ wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip
$ unzip terraform_0.11.7_linux_amd64.zip
$ mv terraform /usr/local/bin/terraform
GO
The libvirt plugin README.md warns that it depends on recent features of go, so I installed the latest stable version 1.10.3 as follows
$ wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
$ tar -zxvf go1.10.3.linux-amd64.tar.gz
$ sudo mv go /usr/local
$ vi ~/.bashrc
GOROOT=/usr/local/go
GOPATH=${HOME}/go
PATH=${GOROOT}/bin:${GOPATH}/bin:$PATH
export GOROOT GOPATH PATH
$ source ~/.bashrc
$ mkdir ~/go
$ go version
go version go1.10.3 linux/amd64
terraform-libvirt-plugin
The libvirt plugin depends on terraform-providers
$ go get github.com/hashicorp/terraform/plugin
$ go install github.com/hashicorp/terraform/plugin
$ yum insall libvirt-devel
$ mkdir -p ~/.terraform.d/plugins
$ cd ~/.terraform.d/plugins
$ go get -v -u github.com/dmacvicar/terraform-provider-libvirt
$ go build -a -v github.com/dmacvicar/terraform-provider-libvirt
Testing
With the plugin installed, I was able to create resources with terraform+libvirt
$ mkdir ~/test-tf
$ cd ~/test-tf
$ vi libvirt.tf
provider "libvirt" {
uri = "qemu:///system"
}
resource "libvirt_domain" "terraform_test" {
name = "terraform_test"
}
$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy