Information¶
I have been lurking at Infrastructure as Code (IaC) for a while. I am using a proxmox server at work to setup a small network that i use to teach network security.
At the moment everything is very manual with a lot of clicking around in the proxmox gui and the VM command lines.
My goal is to be able to spin up an entire lab environment in a few minutes using terraform to provision infrastructure and Ansible to configure hosts and network devices.
My journey towards this begins with a writeup of the video by Learn Linux TV:
Provisioning Virtual Machines in Proxmox with Terraform – Full Walkthrough
I did tweak the main.tf
file to comply with recent changes to the provider plugin telmate/proxmox
and no guarantees are made that this will work in your environment.
Consider this guide as a crude working example of using terraform to provision a vm in proxmox, and beware that you probably need to tweak it to your needs, before using it.
There are a few prerequisites:
- proxmox server
- proxmox VM template
Proxmox permissions and access token¶
- Copy the URL of your proxmox server ie.
https://10.10.10.10:8006
- Copy name of the VM template ie.
debian-12
Setup proxmox ressources¶
- click datacenter
- go to users
- add user - do not configure anything other than the name
TerraformUser
- go to roles
- create new role call it
TerraformProvision
and add these permissions:
Datastore.AllocateSpace Datastore.Audit Pool.Allocate SDN.Use Sys.Audit Sys.Console Sys.Modify Sys.PowerMgmt VM.Allocate VM.Audit VM.Clone VM.Config.CDROM VM.Config.CPU VM.Config.Cloudinit VM.Config.Disk VM.Config.HWType VM.Config.Memory VM.Config.Network VM.Config.Options VM.Migrate VM.Monitor VM.PowerMgmt
- click groups
- create a group called
TerraformPermissions
- click permissions -> add group permissions
- add the following:
- edit
TerraformUser
and add groupTerraformPermissions
to it
Create proxmox API token¶
- Click
API tokens
- Select the
TerraformUser
- Give the Token a name ie.
TerraformToken
- Uncheck
Privelege separation
- Click
Add
- Save the
Token ID
and theSecret
in a safe place like your vault or password manager
DO NOT EXPOSE THIS IN A GIT REPO OR OTHER PUBLIC PLACE
Terraform setup and VM configuration¶
These steps sets up terraform on a local Linux machine, this can be a VM or physical machine.
After setup of terraform the creation of a VM using terraform is explained.
Prepare Terraform on local machine¶
TODO: Add section about protecting secrets from version control
- go to https://developer.hashicorp.com/terraform/install
- copy the download link that fits your processor, ie. AMD64
- switch to a linux terminal and
WGET
the link
- use the terminal to unzip the downloaded file
- if on a multi user system you can change ownership to ie.
root
withchown root:root terraform
- To use the
terraform
command you need to move the file with the command
sudo mv terraform /usr/local/bin/
- Check that the path is recognized by typing
command -v terraform
and confírm that the output is/usr/local/bin/terraform
(this confirms that theterraform
command is available)
Create terraform files¶
- create a directory for terraform in the home directory
mkdir ~/terraform
- create a file called
main.tf
in the~/terraform
directory - open the
main.tf
file in your favorite editor and add the following: - replace the
your-token-id
andyour-secret
with the token id and secret from the previous steps
DISCLAIMER: DO NOT EXPOSE TOKEN ID AND SECRET IN A GIT REPO OR OTHER PUBLIC PLACE
PLEASE USE A VARIABLES FILE, ENVIRONMENT VARIABLES OR HASHICORP VAULT TO MANAGE THIS INFORMATION OUTSIDE OF VERSION CONTROL
FOR ADDTIONAL INFORMATION SEE:
https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables - replace the
your-proxmox-node-name
with the name of your proxmox node - replace the
your-template-name
with the name of your VM template - check that the
vmbr1
corresponds to your desired network bridge in proxmox
Run terraform¶
- go to the
~/terraform
directory - run
terraform init
- run
terraform plan
to check what will be changed (this does not cjange anything in your proxmox node)
- run
terraform apply
this will attempt to create the VM in proxmox
- check that the VM is created in the proxmox GUI