0

I am passing a map from terraform code to jinja2 for creating an ansible's hosts file. In the hosts file, I am printing the map using the following code and it is working fine.

1. %{ for key, value in cat ~}
2. [${ key }]
3. %{ for v in value ~} 
4. ${ v }
5. %{ endfor ~}

6. %{ endfor ~}

I am now trying to append :children with the key but it is not working. I tried the following 04 ways of changing the line 2 in the above code but no luck. Following the link String concatenation in Jinja

Attempt 1:
[${ key|join(":children") }]

Attempt 2:
[${ key + ':children' }]

Attempt 3:
[${ key ~ ':children' }]

Attempt 4:
[${ key ~ ':children' ~}]

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
  • Not tested, but -I don’t understand addressing variables using `$` in Jinja... I know only addressing via ```{{ variable }}```... -I would escape square brackets. Below I escape also complete `:children]` part: So maybe following will work: ```{{ '[' }} {{ key }} {{ ':children]' }}``` – Konstantin Volenbovskyi Apr 03 '23 at 15:52
  • 1
    @KonstantinVolenbovskyi this is a Terraform template, so it is their own DSL. – β.εηοιτ.βε Apr 03 '23 at 15:54
  • `["${key}:children"]` should be what you are looking for. – β.εηοιτ.βε Apr 03 '23 at 15:55
  • Does this answer your question? [Terraform Combine Variable and String](https://stackoverflow.com/questions/54752049/terraform-combine-variable-and-string) – β.εηοιτ.βε Apr 03 '23 at 15:56
  • I tried that now `[${ key }:children]` and so far so good. I dont know if it will have a space or not i.e. `key :children` or will it be `key:children` the later is what I want. I will try `["${key}:children"]` that after above run – Dr. Mian Apr 03 '23 at 16:07

1 Answers1

0

[${ key }:children] that did the trick so the complete answer will be

%{ for key, value in cat ~}
[${ key }:children]
{ for v in value ~} 
${ v }
%{ endfor ~}

%{ endfor ~}

Which will give us

[blue:children]
[green:children]
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69