Hi @cello86,
I understand your question as how to construct a list which has two entries for each element of this map, each of which is itself a list of arguments to run.
I think the following expression would achieve that:
locals {
dir_commands = concat([
for path, owner in var.dirs : [
["mkdir", "-p", path],
["chown", "-R", owner, path],
]
]...)
}
The above works in two steps:
- The
for
expression first produces a list of lists, where the top-level list has one element per element of your map and each contains two elements representing the two comments.
- The
concat
function takes all of the first-level lists as individual arguments (due to using the ...
expansion symbol at the end of its argument) and so removes that first level of lists so all of the commands for all of the paths are together in a single list.
With this, I would expect local.dir_commands
to have the following value:
[
["mkdir", "-p", "/app"],
["chown", "-R", "jboss", "/app"],
["mkdir", "-p", "/test"],
["chown", "-R", "root", "/test"],
["mkdir", "-p", "/var/logs/test"],
["chown", "-R", "root", "/var/logs/test"],
]
You can then use that as part of a call to yamlencode
to produce the full cloud-init YAML document:
locals {
cloudinit_config = <<-EOT
#!cloud-config
${yamlencode({
runcmd = local.dir_commands
# ...
})}
EOT
}