For building the templates I took huge inspiration from the following projects:
- https://github.com/vmware-samples/packer-examples-for-vsphere (doc: https://vmware-samples.github.io/packer-examples-for-vsphere/getting-started)
- https://github.com/ChristianLempa/boilerplates/tree/main/packer/proxmox
Install packer: https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install packer
Install packer plugins:
packer plugins install github.com/hashicorp/proxmox
Install Terraform: https://developer.hashicorp.com/terraform/install
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Install Ansible requirements:
python3 -m pip install -r requirements.txt --break-system-packages
Validate the configuration:
packer validate -var-file='environments/example.pkr.hcl' 'builds/ubuntu/24-04-LTS/.'
And then apply it:
packer build -var-file='environments/example.pkr.hcl' 'builds/ubuntu/24-04-LTS/.'
Per sostituire il template già presente utilizzare l'opzione -force
.
Next, run the command terraform init. This will initialize a new working directory for use with Terraform. More can be read about this command here.
cd terraform
terraform init
Now that we have an initialized project, let's run a terraform plan command. This will give us output indicating what will be created, modified, or destroyed by Terraform. It is generally a good idea to specify an -out file to save this generated plan to, rather than relying on a speculative plan.
cd terraform
terraform plan --var-file='../environments/example.pkr.hcl' -out plan
After all of your hard work, you are now ready to apply your plan and spin up a VM within your Proxmox server! Doing so is as simple as running the command terraform apply plan. This will use our saved plan file and apply it.
Assuming things run successfully, you should see the following output: Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform apply plan
To minimize repetition of credentials and configurations, we try to reuse the same configuration files. The following command load the Proxmox credentials from the tfvars configuration file.
export $(grep -v " " "../environments/example/example.pkr.hcl" | xargs) && ansible-inventory -i ../environments/example/ansible --list