Search code examples
terraformhashicorp-vaultterraform-provider-gcpvaulthashicorp

Terraform array variable to string from vault


I want to use an array to be used as a secret in the vault, but the required value is not as expected it carries the backslash \ character after being output in terraform. i tried to use trim in terraform but it just erased some backslashes and the value can't be used

secret iplist from vault ui secret iplist from vault ui

result outputs from terraform result outputs from terraform

output trim code trim(nonsensitive(data.secret.secret_data.data["iplist"]) , "\"")

result outputs after using trim result outputs after using trim

can I use the value in the form of an array without the backslash \ and in the form of a list of strings ?


Solution

  • I achieved a similar result by using format with %v which formats arrays, I understood that you are getting your value like the following local variable:

    locals {
      ips = "\"10.10.1.0/24\",\"10.10.2.0/24\",\"10.10.1.1/32\""
    }
    

    You only need the square brackets [] to form an array string, then pass it to format

    output "ips" {
      value = format("%v", "[${local.ips}]")
    }
    

    And this was the result

      + ips = jsonencode(
            [
              + "10.10.1.0/24",
              + "10.10.2.0/24",
              + "10.10.1.1/32",
            ]
        )
    

    The previous plan result showed jsonencode since it printed a valid JSON array so I wanted to try it directly, and it worked:

    output "ips" {
      value = jsondecode("[${local.ips}]")
    }
    

    The plan:

      + ips = [
          + "10.10.1.0/24",
          + "10.10.2.0/24",
          + "10.10.1.1/32",
        ]