terraformyamlterraform-template-file

How to retrieve a list from a list of dictionary passed to yamldecode function


I am new to terraform but I thought this should be very simple process however no example on internet. I have specs of a vm

abc:
  - vm_name: "abc"
    template:
      cat: Apple
      image: "ubuntu18"
    cpu_cores: 1
    memory: 1024
    cat:
      - red
      - geen
      - blue

This data is assigned to a variable using

cfg_vars = yamldecode(file(var.env_config))

All I want is to get cat list members (I think it is a list) i.e. red, green and blue in a variable list_color.

I checked splat [*] operator, but it is for map I believe. I assigned directly using

list_color = try(cfg_vars.abc.groups, [])

But it is not working. How can I get the values in terraform?


Solution

  • You can use splat (*) expression:

    locals {
    
      cfg_vars = yamldecode(file("test1.yaml"))
      list_color = flatten(local.cfg_vars["abc"][*]["cat"])
    
    }