Hi everyone!
I try to use terraform + proxmox cloud-init stuff
I have main.tf with this config:
terraform {
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "2.9.2"
}
}
}
provider "proxmox" {
pm_api_url = "https://10.1.0.76:8006/api2/json"
pm_api_token_id = "terra_user@pam!new_token_id"
pm_api_token_secret = "a9c362d5-3021-41e5-a0cb-sssseeeeccrreeett"
pm_tls_insecure = true
}
data "template_file" "cloud_init_centos-nginx" {
template = "${file("${path.module}/templates/centos-nginx.cloud_config")}"
vars = {
ssh_key = file("~/.ssh/id_rsa.pub")
hostname = "test-centos-10"
domain = "DOMAIN"
}
}
resource "local_file" "cloud_init_centos-nginx" {
content = data.template_file.cloud_init_centos-nginx.rendered
filename = "${path.module}/templates/one.yml"
}
resource "null_resource" "cloud_init_centos-nginx" {
connection {
type = "ssh"
user = "root"
private_key = file("~/.ssh/id_rsa")
host = "HOST"
}
provisioner "file" {
source = "templates/one.yml"
destination = "/var/lib/vz/snippets/one.yml"
}
}
resource "proxmox_vm_qemu" "test_centos" {
depends_on = [
null_resource.cloud_init_centos-nginx,
]
count = 1
name = "test-centos-${count.index + 10}"
target_node = var.proxmox_host
clone = var.template_name
agent = 1
os_type = "cloud-init"
cores = 2
sockets = 1
cpu = "host"
memory = 2048
scsihw = "virtio-scsi-single"
bootdisk = "scsi0"
cicustom = "user=local:snippets/one.yml"
ipconfig0 = "ip=10.1.0.125/24,gw=10.1.0.1"
}
… and
provisioner "file" {
source = "templates/one.yml"
destination = "/var/lib/vz/snippets/one.yml"
}
don`t working.
No copying to dest.
File with cloud init config renders with right name, and nothing more.
I tried change paths and other params, but terraform just ignoring all about copying.
What i did wrong?
Thanks all!
1º To make it work easier, prepare your vm template with cloud init:
-Hard way
Normal iso install, install cloud-init, install qemu-guest-agent, clean machine ids and /etc/ssh_host*, add cloud init hardware and make a template.
Normal way
Cloud-init iso install, set login/ssh, install qemu-guest-agent, clean machine ids and /etc/ssh_host* and make a template.
Easy way
2º Terraform code:
credentials.auto.tfvars
proxmox_api_url = “https://pve1.my.local:8006/api2/json”
proxmox_api_token_id = “user@pam!terraform”
proxmox_api_token_secret = “fsafjlkgjlarc”
provider.tf
terraform {
required_version = ">= 1.3.6"
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "2.9.11"
}
}
}
variable “proxmox_api_url” {
type = string
}
variable “proxmox_api_token_id” {
type = string
sensitive = true
}
variable “proxmox_api_token_secret” {
type = string
sensitive = true
}
provider “proxmox” {
pm_api_url = var.proxmox_api_url
pm_api_token_id = var.proxmox_api_token_id
pm_api_token_secret = var.proxmox_api_token_secret
pm_timeout = "3600" // I need 30 min couse I'm using a hard disk, but ssd 5 min is enough
pm_log_enable = true
pm_log_file = "terraform-plugin-proxmox.log"
pm_log_levels = {
_default = "debug"
_capturelog = ""
}
pm_tls_insecure = true
}
resources.tf
resource “proxmox_vm_qemu” “ad1” {
name = “ad1”
desc = “Ubuntu Server”
vmid = “211”
target_node = “pve1”
agent = 1
clone = "32Gubuntu-2004"
full_clone = true
cores = 1
sockets = 1
cpu = "host"
memory = 1024
timeouts {
create = "30m"
delete = "30m"
}
network {
bridge = "vmbr0"
model = "virtio"
}
disk {
storage = "disk1"
type = "virtio"
size = "32G"
}
os_type = "cloud-init"
ipconfig0 = "ip=192.168.0.11/24,gw=192.168.0.1"
nameserver = "192.168.0.1"
ciuser = "user"
onboot = true
sshkeys = <<EOF
ssh-rsa sgarhernatrskpNKftckdslagjlagjalekrjglakjglk= user@sakjflsjf
EOF
}
To make it even better, a custom config could be made to install qemu-guest-agent, but i don’t know how to create/use snippets
https://pve.proxmox.com/pve-docs/qm.1.html
The custom config files have to be on a storage that supports snippets and have to be available on all nodes the VM is going to be migrated to. Otherwise the VM won’t be able to start. For example:
qm set 9000 --cicustom “user=local:snippets/userconfig.yaml”
It doesn’t work for me. I don’t know why ![:frowning: :frowning:](https://emoji.discourse-cdn.com/twitter/frowning.png?v=12)
Terraform main.tf
cicustom = "user=storage:snippets/user-data.yml"
cloudinit_cdrom_storage = "storage"
user-data.yml:
users:
- name: szuszi
groups: users, admin
ssh_authorized_keys:
packages:
- qemu-guest-agent