Looping over map key:value pairs? #2458
Answered
by
pd93
IAMSolaara
asked this question in
Q&A
|
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 |
Answered by
pd93
Oct 16, 2025
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 |
0 replies
|
@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}}" |
1 reply
Answer selected by
IAMSolaara
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.