Here's the best I've got to offer. I tried pipelining filters etc but
couldn't quite get over the hump. So, back to old school loops.
- set_fact:
backup_objects: |
{% set result = [] %}
{% for obj in all_objects %}
{% set _ = result.append({'basename': obj['Key'] |
regex_replace('^' ~ prefix ~ '\\d{10}_(.*)\.pgdump', '\\1')} | combine(obj)) %}
{% endfor %}{{ result }}
The point of the "set _" line is for the side effect of appending
revised objects to the "result" list. Otherwise it's pretty straightforward.
Cheers,
--
Todd
On 8/16/23 10:52 AM, Dick Visser wrote:
Hii
I have a list of dicts, where I want to inject a number of helper keys.
I can do this with set_facts in a loop. Example playbook (hopefully
this displays OK):
---
- name:Add helper keys to list of dicts
hosts:localhost
connection:local
gather_facts:no
tasks:
- set_fact:
backup_objects:"{{ backup_objects|default([])|union(
[
item|combine(
{
'basename':item.Key|regex_replace('^'~ prefix~
'\\d{10}_(.*)\\.pgdump','\\1')
}
)
]
)
}}"
loop:"{{ all_objects}}"
loop_control:
label:"{{ item.Key}}"
- debug:var=backup_objects
vars:
prefix:backup/database/
all_objects:
- ETag:'"d41d8cd98f00b204e9800998ecf8427e"'
Key:backup/database/1689953756_dev_wss_db.pgdump
LastModified:'2023-07-21T15:36:01.000Z'
Owner:
ID:2d8917bbcab5a8e0d3d7f5f39d147cd6de38e883357d7ae16323398c302fe97e
Size:0
StorageClass:STANDARD
- ETag:'"d41d8cd98f00b204e9800998ecf8427e"'
Key:backup/database/1689953756_dev_wss_db_requests.pgdump
LastModified:'2023-07-21T15:36:08.000Z'
Owner:
ID:2d8917bbcab5a8e0d3d7f5f39d147cd6de38e883357d7ae16323398c302fe97e
Size:0
StorageClass:STANDARD
- ETag:'"d41d8cd98f00b204e9800998ecf8427e"'
Key:backup/database/1689953756_dev_bss_service_database.pgdump
LastModified:'2023-07-21T15:36:13.000Z'
Owner:
ID:2d8917bbcab5a8e0d3d7f5f39d147cd6de38e883357d7ae16323398c302fe97e
Size:0
StorageClass:STANDARD
- ETag:'"d41d8cd98f00b204e9800998ecf8427e"'
Key:backup/database/1689953756_dev_bss_frontend_db.pgdump
LastModified:'2023-07-21T15:36:19.000Z'
Owner:
ID:2d8917bbcab5a8e0d3d7f5f39d147cd6de38e883357d7ae16323398c302fe97e
Size:0
StorageClass:STANDARD
- ETag:'"d41d8cd98f00b204e9800998ecf8427e"'
Key:backup/database/1689953756_dev_mss_db.pgdump
LastModified:'2023-07-21T15:36:25.000Z'
Owner:
ID:2d8917bbcab5a8e0d3d7f5f39d147cd6de38e883357d7ae16323398c302fe97e
Size:0
StorageClass:STANDARD
This works, but in my real world use case this list is very long, so
there will be a lot of output because of each iteration. I have
already set the loop label to something less noisy.
Is there a way to add keys to a list of dicts, where those new keys
are based on an operation of another key?
thx !
Dick
--
You received this message because you are subscribed to the Google
Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAF8BbLZDy-7ym4pcEJSBuCVkZZ2gPzinj8NFTcNnN6WedH%2BfMQ%40mail.gmail.com
<https://groups.google.com/d/msgid/ansible-project/CAF8BbLZDy-7ym4pcEJSBuCVkZZ2gPzinj8NFTcNnN6WedH%2BfMQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
--
Todd
--
You received this message because you are subscribed to the Google Groups "Ansible
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/5bd7bec0-540c-3505-cbfd-75b47e9869ff%40gmail.com.