diff --git a/README.md b/README.md index 403478ed..26e73475 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,84 @@ EOF } } +/* Null resource that generates a cloud-config file per vm */ +data "template_file" "user_data" { + count = var.vm_count + template = file("${path.module}/files/user_data.cfg") + vars = { + pubkey = file("~/.ssh/id_rsa.pub") + hostname = "vm-${count.index}" + fqdn = "vm-${count.index}.${var.domain_name}" + } +} +resource "local_file" "cloud_init_user_data_file" { + count = var.vm_count + content = data.template_file.user_data[count.index].rendered + filename = "${path.module}/files/user_data_${count.index}.cfg" +} + +resource "null_resource" "cloud_init_config_files" { + count = var.vm_count + connection { + type = "ssh" + user = "${var.pve_user}" + password = "${var.pve_password}" + host = "${var.pve_host}" + } + + provisioner "file" { + source = local_file.cloud_init_user_data_file[count.index].filename + destination = "/var/lib/vz/snippets/user_data_vm-${count.index}.yml" + } +} + +/* Configure cloud-init User-Data with custom config file */ +resource "proxmox_vm_qemu" "cloudinit-test" { + depends_on = [ + null_resource.cloud_init_config_files, + ] + + name = "tftest1.xyz.com" + desc = "tf description" + target_node = "proxmox1-xx" + + clone = "ci-ubuntu-template" + + # The destination resource pool for the new VM + pool = "pool0" + + storage = "local" + cores = 3 + sockets = 1 + memory = 2560 + disk_gb = 4 + nic = "virtio" + bridge = "vmbr0" + + ssh_user = "root" + ssh_private_key = <] [,gw6=] [,ip=] [,ip6=] * ipconfig1 - optional, same as ipconfig0 format +Alternatively, cloud-init configuration can be customized with config files that reside in a volume in the Proxmox VE server. Use the attribute `cicustom` to indicate the location of these files. + +* cicustom - location of cloud-config files that Proxmox will put in the generated cloud-init config iso image, e.g. `cicustom = "user=local:snippets/user_data.yaml"`. For more info about this attribute, see the details of the parameter `--cicustom` in the section "Custom Cloud-Init Configuration" from the [Proxmox Cloud-Init support](https://pve.proxmox.com/wiki/Cloud-Init_Support) page. + ### Preprovision (internal alternative to Cloud-Init) There is a pre-provision phase which is used to set a hostname, intialize eth0, and resize the VM disk to available space. This is done over SSH with the ssh_forward_ip, ssh_user and ssh_private_key. diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index d9382f3b..8289d742 100644 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -434,6 +434,10 @@ func resourceVmQemu() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "cicustom": { + Type: schema.TypeString, + Optional: true, + }, "searchdomain": { Type: schema.TypeString, Optional: true, @@ -525,6 +529,7 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error { // Cloud-init. CIuser: d.Get("ciuser").(string), CIpassword: d.Get("cipassword").(string), + CIcustom: d.Get("cicustom").(string), Searchdomain: d.Get("searchdomain").(string), Nameserver: d.Get("nameserver").(string), Sshkeys: d.Get("sshkeys").(string), @@ -730,6 +735,7 @@ func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error { // Cloud-init. CIuser: d.Get("ciuser").(string), CIpassword: d.Get("cipassword").(string), + CIcustom: d.Get("cicustom").(string), Searchdomain: d.Get("searchdomain").(string), Nameserver: d.Get("nameserver").(string), Sshkeys: d.Get("sshkeys").(string), @@ -831,6 +837,7 @@ func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error { // Cloud-init. d.Set("ciuser", config.CIuser) d.Set("cipassword", config.CIpassword) + d.Set("cicustom", config.CIcustom) d.Set("searchdomain", config.Searchdomain) d.Set("nameserver", config.Nameserver) d.Set("sshkeys", config.Sshkeys)