Troubleshooting Proxmox Terraform Error Source Must Be Set On Creation

by JurnalWarga.com 71 views
Iklan Headers

Introduction

Hey guys! Today, we're diving into a peculiar issue encountered while creating cloud-init VMs using Terraform with the Proxmox provider. Specifically, we'll be dissecting the "Error: source must be set on creation" that popped up in version 3.0.2-rc02. This issue was brought to light by a user who experienced this hiccup while deploying VMs from a template. Interestingly, the same Terraform configuration worked flawlessly on 3.0.2-rc01 and 3.0.1, pointing towards a potential bug in the newer release. Let's get into the details and see how we can understand and potentially resolve this issue.

The Problem: "Error: source must be set on creation"

So, what's this error all about? The error message "Error: source must be set on creation" typically indicates that a required source attribute is missing during the creation of a resource. In the context of the proxmox_vm_qemu resource in Terraform, this usually refers to the source from which the VM is being cloned or created, such as a template or an existing VM. However, in this case, the user reported that their Terraform file seemed correct, and the issue only surfaced in version 3.0.2-rc02 of the Proxmox provider. This suggests that the problem might not be in the configuration itself, but rather a bug or change in the provider's code in the newer release candidate.

When you encounter this error, it's essential to first double-check your Terraform configuration. Make sure that all the required attributes, especially those related to the source of the VM, are correctly set. This includes parameters like clone (the name of the template to clone from) and full_clone (whether to create a full clone or a linked clone). If these are correctly configured and the error persists, it's a strong indicator that there might be an issue with the provider itself.

Detailed Error Context

The error manifested during the execution of the terraform apply command. The specific error message and the relevant snippet from the Terraform configuration are as follows:

β•·
β”‚ Error: source must be set on creation
β”‚ 
β”‚   with proxmox_vm_qemu.test-from-terraform,
β”‚   on main.tf line 17, in resource "proxmox_vm_qemu" "test-from-terraform":
β”‚   17: resource "proxmox_vm_qemu" "test-from-terraform" {
β”‚ 
β•΅

This error occurred within the proxmox_vm_qemu resource block, specifically for the resource named test-from-terraform. The error message suggests that the source attribute, which is crucial for creating a VM from a template or cloning an existing VM, was not properly set during the creation process. The error trace points to line 17 of the main.tf file, which is where the resource block for proxmox_vm_qemu test-from-terraform begins.

Terraform Configuration Snippet

Here’s a snippet of the Terraform configuration that was used. This configuration defines a proxmox_vm_qemu resource, which is responsible for creating and managing virtual machines on Proxmox. Let's break down the important parts of this configuration to understand what's going on.

resource "proxmox_vm_qemu" "test-from-terraform" {
  vmid = var.vmid
  name = var.name

  target_node = var.proxmox_node

  agent = 1

  cpu {
    cores = var.cores
  }

  memory = var.memory
  boot   = var.boot
  scsihw = var.scsihw

  clone      = var.clone
  full_clone = true

  vm_state         = "running"
  automatic_reboot = true

  cicustom   = var.cicustom
  ciupgrade  = var.ciupgrade
  nameserver = var.nameserver

  ipconfig0 = "ip=${var.ip_address}/24,gw=${var.gateway}"

  skip_ipv6 = true

  ciuser     = var.ciuser
  cipassword = var.cipassword
  sshkeys    = var.sshkeys

  disks {
    virtio {
      virtio0 {
        disk {
          storage = var.storage_pool
          size    = "3G"
        }
      }
    }
    ide {
      ide2 {
        cloudinit {
          storage = var.storage_pool
        }
      }
    }
  }

  network {
    id     = 0
    bridge = "vmbr0"
    model  = "virtio"
    tag    = 4
  }
}

In this configuration, several key attributes are defined:

  • vmid: The virtual machine ID.
  • name: The name of the virtual machine.
  • target_node: The Proxmox node where the VM will be created.
  • clone: The name of the template to clone from. This is a crucial attribute for creating a VM from a template.
  • full_clone: Specifies whether to create a full clone or a linked clone. In this case, it's set to true.
  • cicustom, ciupgrade, nameserver, ipconfig0, skip_ipv6, ciuser, cipassword, and sshkeys: These attributes configure cloud-init, which is used to initialize the VM on first boot.
  • disks: Defines the disk configuration, including a virtio disk and a cloudinit disk.
  • network: Configures the network settings for the VM.

The fact that the clone attribute is set suggests that the intention is to create a VM by cloning from a template. The full_clone attribute being set to true indicates that a full clone should be created, which means that all the data from the source template will be copied to the new VM.

Variables

The Terraform configuration also uses several variables, such as var.vmid, var.name, var.proxmox_node, var.cores, var.memory, var.boot, var.clone, var.cicustom, var.ciupgrade, var.nameserver, var.ip_address, var.gateway, var.ciuser, var.cipassword, var.sshkeys, and var.storage_pool. These variables are used to parameterize the configuration, making it more flexible and reusable.

The user confirmed that all these variables were correctly set, so the issue is unlikely to be related to incorrect variable values.

Reproduction Steps

To reproduce the issue, the user followed these steps:

  1. Use Terraform v1.12.1.
  2. Configure the Proxmox provider with version 3.0.2-rc02.
  3. Define a proxmox_vm_qemu resource with the clone and full_clone attributes set.
  4. Run terraform apply.

The error "Error: source must be set on creation" occurs during the apply process.

The user also confirmed that when they switched back to version 3.0.2-rc01, the same Terraform configuration worked without any issues. This further supports the hypothesis that the bug is specific to version 3.0.2-rc02.

Solution / Workaround

Downgrading the Proxmox Provider

The immediate workaround for this issue is to downgrade the Proxmox provider version in your Terraform configuration. As the user discovered, switching back to version 3.0.2-rc01 resolves the error. To do this, modify your terraform block in the main.tf file as follows:

terraform {
  required_providers {
    proxmox = {
      source  = "Telmate/proxmox"
      version = "3.0.2-rc01" # Changed from rc02
    }
  }
}

By specifying the version attribute, you can pin the provider to a specific version. In this case, we're pinning it to 3.0.2-rc01, which is known to work correctly with the given configuration. After making this change, run terraform init to reinitialize the provider with the specified version. This will download and install the correct version of the Proxmox provider, and you should be able to run terraform apply without encountering the error.

Reporting the Issue

While downgrading the provider is a viable workaround, it's crucial to report the issue to the provider maintainers. This helps them identify and fix the bug in a future release. You can report the issue by creating a new issue on the Terraform Proxmox provider GitHub repository. When reporting the issue, be sure to include the following information:

  • Terraform version
  • Proxmox provider version
  • Terraform configuration
  • Error message
  • Steps to reproduce the issue

Providing detailed information makes it easier for the maintainers to understand and address the problem.

Further Debugging Steps

If you want to further investigate the issue, you can try enabling debug logging in Terraform. This can provide more detailed information about what's happening behind the scenes and might help pinpoint the root cause of the error. To enable debug logging, set the TF_LOG environment variable to DEBUG:

export TF_LOG=DEBUG
terraform apply

The debug logs will be quite verbose, but they can contain valuable information about the provider's behavior. Look for any error messages or warnings that might provide clues about the issue. You can also share the debug logs with the provider maintainers when reporting the issue.

Additional Information

Terraform and Proxmox Versions

  • Terraform Version: v1.12.1
  • Proxmox Provider Version: 3.0.2-rc02 (buggy), 3.0.2-rc01 (working), 3.0.1 (working)

Key Attributes in the Terraform Configuration

  • clone: Specifies the template to clone from.
  • full_clone: Set to true, indicating a full clone should be created.

Affected Resource

  • proxmox_vm_qemu: The resource used to create and manage Proxmox virtual machines.

Conclusion

The "Error: source must be set on creation" in Proxmox provider version 3.0.2-rc02 is a tricky issue that can halt your cloud-init VM deployments. But don't worry, guys! By downgrading to version 3.0.2-rc01, you can keep your infrastructure running smoothly. Remember, it's super important to report bugs like this to the maintainers so they can fix them in future releases. Happy Terraforming, and may your VMs always spin up without a hitch!

By understanding the error, the configuration, and the steps to reproduce it, you are better equipped to troubleshoot and resolve similar issues in the future. Additionally, contributing to the community by reporting bugs and sharing your findings helps improve the overall quality and stability of the Proxmox provider.