Looping over map key:value pairs? #2458
-
|
Hello, I have this task declared: tasks:
map_test:
vars:
NODES_MAP:
map:
foo: { type: a }
bar: { type: a }
baz: { type: b }
cmd:
for: { var: NODES_MAP }
cmd: "echo map key: {{.ITEM}}"And thus get as output: $ task map_test
task: [map_test] echo map key: map[type:a]
map key: map[type:a]
task: [map_test] echo map key: map[type:a]
map key: map[type:a]
task: [map_test] echo map key: map[type:b]
map key: map[type:b]which just gives me the values, but not the keys. Is there a way to loop over a key:value pair, like a record? Am I missing something? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I guess one way to do this would be turning the map into a list of maps, thus getting: NODES:
- map:
key: foo
type: a
- map:
key: bar
type: a
- map:
key: baz
type: b |
Beta Was this translation helpful? Give feedback.
-
|
@IAMSolaara When looping over maps, we make the tasks:
map_test:
vars:
NODES_MAP:
map:
foo: { type: a }
bar: { type: a }
baz: { type: b }
cmd:
for: { var: NODES_MAP }
cmd: "echo map key: {{.KEY}} - {{.ITEM}}" |
Beta Was this translation helpful? Give feedback.
@IAMSolaara When looping over maps, we make the
{{.KEY}}variable available for use. You can see this in our looping over variables documentation. e.g.