Worth Reading: Data Manipulation in Jinja2
Ansible and Jinja2 are not an ideal platform for data manipulation, but sometimes it’s easier to hack together something in Jinja2 than writing a Python filter. In those cases, you might find the Data Model Transformation with Jinja2 by Philippe Jounin extremely useful.
Couldn't get the selectattr example to work
{% for route in routes|selectattr('mode','equalto','backup')|map('combine', { 'distance': '150' }) + routes|selectattr('mode','equalto','nominal')|map('combine', { 'distance': '10' })......
Kept giving "unsupported operand type(s) for +: 'generator' and 'generator'" error
Based on your error message, I'm suspecting you need a list after the map (
map(...)|list
) to convert a generator into a list.Not sure whether something changed in the meantime or whether it's a bug in the original document.