Cant parse a variable using yamldecode

In my terraform file, I have the following variable:

variable "pm_target_node" {   
    default = "$yamldecode(file("hostnames.yml"))["hostnames"]" 

this hostnames.yml file corresponds to the following content:

    - "shiloh"
    - "cato"

When I run my terraform plan, I get the following error:

│ Error: Missing newline after argument 
│   on line 6, in variable "pm_target_node": 
│    6:   default = "$yamldecode(file("hostnames.yml"))["hostnames"]" 
│ An argument definition must end with a newline.

Following the instructions from the Terraform Docs for yamldecode, I dont see an option to present data as content from an array, which is what I want. In the end, I want the data shown in that variable to be injected into a vm generating resource for proxmox, like so:

resource "proxmox_vm_qemu" "domain_controllers" {   
    vmid              = "100${count.index}"   
    count             = var.pm_count      
    name              = "dc-${count.index}.${var.pm_domain_name}"   
    target_node       = var.pm_target_node[count.index]   
    clone             = var.pm_almatemplate_name

Any thoughts on how to get that yamldecode to parse my hostnames array would be awesome.

It’s a weird error, but the first thing to do is to fix your incorrect syntax which might be causing it. You have a random extra "$ at the start and " at the end.

It should be

default = yamldecode(file("hostnames.yml"))["hostnames"]

Hey @maxb, thank you for that input! You actually kinda solved my problem. Once I made the changes you recommended, it started throwing new and exciting errors at me, saying I can’t use functions within variables. What I did then is thrown those yamldecode functions directly into my terraform resources, and everything started working (with a bit of formatting). Id love to know why I can’t obfuscate it by keeping those yamldecode functions inside of variables, but I’m reasonably happy with the solution.

For those looking to solve a similar problem, here’s what my code looks like now within my resource file:

resource "proxmox_vm_qemu" "domain_controllers" {
  vmid              = "100${count.index}"
  count             = yamldecode(file("seed-variables.yml"))["hypervisor_parity"]
  name              = "dc-${count.index}.${yamldecode(file("seed-variables.yml"))["domain_name"]}"
  target_node       = yamldecode(file("hostnames.yml"))["hostnames"][count.index]
  clone             = var.pm_almatemplate_name
  os_type           = "cloud-init"

Hi @LittleSeneca,

Input variables are for passing in data from outside rather than for specifying data inside your module. Terraform does allow specifying a default value but it must be a literal value because it exists primarily to tell the user of your module what value will be used if they don’t provide one.

If you want to factor out a complex expression and refer to its results in many places, that is what Local Values are for.