mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Add an 'extract' filter
At its most basic, this is nothing more than an array or hash lookup, but when used in conjunction with map, it is very useful. For example, while constructing an "ssh-keyscan …" command to update known_hosts on all hosts in a group, one can get a list of IP addresses with: groups['x']|map('extract', hostvars, 'ec2_ip_address')|list This returns hostvars[a].ec2_ip_address, hostvars[b].ec2_ip_address, and so on. You can even specify an array of keys for a recursive lookup, and mix string and integer keys depending on what you're looking up: ['localhost']|map('extract', hostvars, ['vars','group_names',0])|first == hostvars['localhost']['vars']['group_names'][0] == 'ungrouped' Includes documentation and tests.
This commit is contained in:
parent
5be98ca91e
commit
8cf1815867
3 changed files with 58 additions and 0 deletions
|
@ -68,3 +68,13 @@
|
|||
- '"0.10 GB" == 102400000|human_readable(unit="G")'
|
||||
- '"0.10 Gb" == 102400000|human_readable(isbits=True, unit="G")'
|
||||
|
||||
- name: Container lookups with extract
|
||||
assert:
|
||||
that:
|
||||
- "'x' == [0]|map('extract',['x','y'])|list|first"
|
||||
- "'y' == [1]|map('extract',['x','y'])|list|first"
|
||||
- "42 == ['x']|map('extract',{'x':42,'y':31})|list|first"
|
||||
- "31 == ['x','y']|map('extract',{'x':42,'y':31})|list|last"
|
||||
- "'local' == ['localhost']|map('extract',hostvars,'ansible_connection')|list|first"
|
||||
- "'local' == ['localhost']|map('extract',hostvars,['ansible_connection'])|list|first"
|
||||
- "'ungrouped' == ['localhost']|map('extract',hostvars,['vars','group_names',0])|list|first"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue