Good afternoon. I have some Terraform code that normally creates the debian-image
VM on Proxmox, but when I run it again, it doesn’t create the VM because one with that name already exists. How can I modify the code so that if a VM with that name already exists, it changes the name?
My provider.tf
terraform {
required_version = ">= 1.6.3"
required_providers {
proxmox = {
version = ">= 2.9.14"
source = "telmate/proxmox"
}
}
}
provider "proxmox" {
pm_api_url = var.pm_api_url
pm_tls_insecure = true
pm_api_token_id = "xxxx-xxxx-xxxx" # Defina apenas uma vez
pm_api_token_secret = "xxxx-xxxx-xxxx" # Defina apenas uma vez
}
my variables.tf
variable "pm_user" {
type = string
description = "Username for Authentication"
default = "root"
}
variable "pm_api_url" {
type = string
description = "API URL: https://192.168.124.2:8006/api2/json"
default = "https://192.168.124.2:8006/api2/json"
}
variable "target_node" {
type = string
description = "The name of the node"
default = "terraform"
}
my vm.tf
resource "proxmox_vm_qemu" "debian" {
name = "debian-image"
desc = "Maquina Debian"
target_node = var.target_node
pool = ""
clone = "debian-11-cloudinit-template"
full_clone = false
scsihw = "virtio-scsi-single"
agent = 1
balloon = 1
cores = 2
sockets = 1
cpu = "host"
memory = 2048
network {
model = "virtio"
bridge = "vmbr0"
firewall = false
link_down = false
}
timeouts {
create = "10m"
delete = "10m"
}
}