Skip to content

Instantly share code, notes, and snippets.

@skarllot
Created March 22, 2024 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skarllot/8ae1d5484417ad6cd954ee2143561b31 to your computer and use it in GitHub Desktop.
Save skarllot/8ae1d5484417ad6cd954ee2143561b31 to your computer and use it in GitHub Desktop.
Terraform multiple replaces
# Ref: https://stackoverflow.com/a/67392622/23266117
locals {
input = file("./sample.txt")
map = {
food = "Sushi"
name = "Data_sniffer"
}
out = join("\n", [
for line in split("\n", local.input) :
format(
replace(line, "/{(${join("|", keys(local.map))})}/", "%s"),
[
for value in flatten(regexall("{(${join("|", keys(local.map))})}", line)) :
lookup(local.map, value)
]...
)
])
}
output "test_out" {
value = local.out
}
Hello {name}, my favorite food is {food} I'd
like to eat {food} and sleep.
Here's a {non_existent} variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment