Is there any way to do what this does with just map
, pipes, and filters instead of the {% for … %}
old school style of looping? (Example borrowed from earlier discussion over on https://matrix.to/#/#users:ansible.com.)
- name: Filter pain point - product
ansible.builtin.debug:
msg: |-
{% for p in starter | map('map', 'list') -%}
{{ p[0] | product(p[1]) -}}
{{ '' if loop.last else ', ' -}}
{% endfor %}
vars:
starter: [ [ [ 11, 12, 13], "a" ],
[ [ 21, 22, 23], "b" ] ]
The result is the product of the number lists with their corresponding letters:
[[[11, "a"], [12, "a"], [13, "a"]], [[21, "b"], [22, "b"], [23, "b"]]]
I keep trying to sneak up on a solution like the one below, but product
only ever takes the first parameter. I can’t get map()
to feed the second element to the filter as the “inside parens” parameter. This isn’t just an issue with product
; it seems to hold for any filter that expects “something left of the pipe” and “something in the parameter list.”
- name: Filter pain point - product
ansible.builtin.debug:
msg:
- "{{ starter }}"
- "{{ starter | map('map', 'list') }}"
- "{{ starter | map('map', 'list') | map('product') }}" # borken!
vars:
starter: [ [ [ 11, 12, 13], "a" ],
[ [ 21, 22, 23], "b" ] ]
# hand-tweaked output:
# [[[[11, 12, 13], 'a'], [[21, 22, 23], 'b']],
# [[[11, 12, 13], ['a']], [[21, 22, 23], ['b']]],
# [[([11, 12, 13],), (['a'],)], [([21, 22, 23],), (['b'],)]]]
Is there really no way to use these filters in complex pipelines?