While the proxmox GUI is great for getting an overview, it is horrible for repetitive tasks and large workloads. We need some automation. We can use Terraform to create the VMs and Ansible to provision the instances.
Terraform is used by EF devops to create and manage instances. Proxmox has a community plugin known as "Telmate/proxmox"
that can be used with Terraform. Have a look at the terraform example in terraform-example/environment/example/main.tf
.
Another detailed example can be found here: https://yetiops.net/posts/proxmox-terraform-cloudinit-saltstack-prometheus/
Before we use terraform, we need to create a template that we can base our images off of. This is similar to an AMI on AWS. Follow this guide for creating an image: https://yetiops.net/posts/proxmox-terraform-cloudinit-saltstack-prometheus/ WARNING!!! After importing the disk image to proxmox storage, you will see a path where the image was imported. Use that path to attach the disk to the virtual machine. Otherwise you will just get storage errors and cannot use the template.
WARNING!!! make sure you set a "unique" name for the template, ideally "name-$NAME-OF-NODE" or so. Having the same template
name across multiple nodes leads to a 500 non-shared storage
error since terraform tries to use a template
that is in a different host. Using unique names and setting it in the resource avoids this problem entirely.
E.g:
Run:
qm importdisk 9001 debian-10-openstack-amd64.qcow2 local
Output: Successfully imported disk as unused0:local:9001/vm-9001-disk-0.raw
And then: qm set 9001 -scsihw virtio-scsi-pci -virtio0 local:9001/vm-9001-disk-0.raw
- Terraform requires a username:password to authenticate itself against the API, so create the requisite pair in the GUI
- Add the
pm_url
,pm_user
andpm_password
directly in themain.tf
OR export them to your env by settingTF_VAR_pm_user
,TF_VAR_pm_password
,TF_VAR_pm_api_url
- Modify the information in your
main.cf
to match your environment and needs. Target node refers to the physical proxmox node on which the VM needs to be created. So you would need one module per region. - Run
terraform init
to init the provider - The terraform file
main.tf
usescloud-init
to provision the instance from a "Template image" and then runs custom scripts,creates users, SSH keys, etc. look atterraform-example/modules/instances/resource.tf
andterraform-example/environment/example/files/cloud_init_deb10.cloud_config
.
Note: Terraform doesn't explicitly create "Tags" with the proxmox provider, instead the tags can be saved as a string in the "Description" field of proxmox.