How to convert a list to a multiline string in terraform?

193 Views Asked by At

I'm using ArgoCD and one of the config params for RBAC accepts a CSV block i.e

policy.csv: |
    p, role:org-admin, applications, *, */*, allow
    p, role:org-admin, clusters, get, *, allow

I'd like to provide a .tfvars file with a list of strings i.e

["p, role:org-admin, applications, *, */*, allow","p, role:org-admin, clusters, get, *, allow"]

And have it convert the list into a multiline CSV output like above, any suggestions?

2

There are 2 best solutions below

0
On BEST ANSWER

Use the simple join function

Code to use in terraform file (.tf)

locals {
policy_csv = join("\n", var.policies)
}
> join("\n",["p, role:org-admin, applications, *, */*, allow","p, role:org-admin, clusters, get, *, allow"])
**output**
<<EOT
p, role:org-admin, applications, *, */*, allow
p, role:org-admin, clusters, get, *, allow
EOT

If you want to add few spaces at the start use below.

# String Output
> join("\n", formatlist("    <Add Black Spaces> %s",tolist(["p, role:org-admin, applications, *, */*, allow","p, role:org-admin, clusters, get, *, allow"])))
**Output**
<<EOT
    p, role:org-admin, applications, *, */*, allow
    p, role:org-admin, clusters, get, *, allow
EOT
2
On

Just use the join function:

locals {
  policy_csv = join("\n", var.policy_list)
}