Reputation: 1361
I'm using Ansible 2.9 on RHEL 7.7, and I'm trying to loop over a list which is a value from a dict element. So far I have this var file and play:
ssh_keys:
account: blah
permissions: 600
keys:
- qa-publickeys['1']
- qa-publickeys['2']
- qa-publickeys['3']
the play:
- name: Traversing ssh keys
debug:
msg: "Here's: {{ item }}"
loop: "{{ ['keys'] | map('extract', ssh_keys) | list }}"
The problem is, msg
is "msg": "Here's: [u\"qa-publickeys['1']\", u\"qa-publickeys['2']\", u\"qa-publickeys['3']\"]"
Why is it not giving me three outputs, with Here's: qa-publickeys['1']
as the first output, Here's: qa-publickeys['2']
as the second, and finally Here's: qa-publickeys['3']
?
The list that I present to the loop
in this play is not getting looped over, it's just iterating once in one chunk.
Upvotes: 1
Views: 337
Reputation: 39119
You seems to make is really complex for yourself for no apparent reason.
A dictionary in Ansible can be accessed via either the .
dot notation or the []
square brackets notation.
Now because .keys()
is indeed a built-in method of a dictionary in Python, you cannot use the first notation, but you can use the later one.
Given the playbook:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "Here's: {{ item }}"
loop: "{{ ssh_keys['keys'] }}"
vars:
ssh_keys:
account: blah
permissions: 600
keys:
- qa-publickeys['1']
- qa-publickeys['2']
- qa-publickeys['3']
This yields the recap:
PLAY [all] *******************************************************************************************************
TASK [debug] *****************************************************************************************************
ok: [localhost] => (item=qa-publickeys['1']) =>
msg: 'Here''s: qa-publickeys[''1'']'
ok: [localhost] => (item=qa-publickeys['2']) =>
msg: 'Here''s: qa-publickeys[''2'']'
ok: [localhost] => (item=qa-publickeys['3']) =>
msg: 'Here''s: qa-publickeys[''3'']'
PLAY RECAP *******************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 1