Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 2.68 KB

2.prepare-terraform-access.md

File metadata and controls

42 lines (34 loc) · 2.68 KB

Setting up proxmox for automation

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

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/

Pre-requisite

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

Instructions

  1. Terraform requires a username:password to authenticate itself against the API, so create the requisite pair in the GUI
  2. Add the pm_url,pm_user and pm_password directly in the main.tf OR export them to your env by setting TF_VAR_pm_user,TF_VAR_pm_password,TF_VAR_pm_api_url
  3. 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.
  4. Run terraform init to init the provider
  5. The terraform file main.tf uses cloud-init to provision the instance from a "Template image" and then runs custom scripts,creates users, SSH keys, etc. look at terraform-example/modules/instances/resource.tf and terraform-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.