Interview Prep/Ansible/Jinja2, Conditionals and Loops

How do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?

IntermediateConceptJinja2, Conditionals and Loops
Answer
vars:
  users:
    - { name: alice, admin: true }
    - { name: bob,   admin: false }
admin_names: "{{ users | selectattr('admin') | map(attribute='name') | list }}"
all_groups:  "{{ [['a','b'], ['b','c']] | flatten | unique }}"
ports:       "{{ services | map(attribute='port') | select('gt', 1024) | list }}"

select/reject filter by a test, selectattr/rejectattr filter dicts by attribute, and map projects values. Most filter results are generators, so add | list to get a real list.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops