Is it possible to flatten a lists of lists with Ansible / Jinja2?

You can do list comprehension to convert it to list of dicts.

For example:

- name: Convert
  shell: python -c "print [x for b in {{ servers }}['servers']['results'] for x in b['tagged_instances']]"
  register: my_list_of_dicts

Assuming that {{ servers }} variable holds the entire dictionary (not json).


A bit late, but starting from ansible 2.5 you can do this:

 "{{ servers.results | map(attribute='tagged_instances') | list | flatten }}"

Jinja2 comes with a built-in filter sum which can be used like:

{{ servers.results | sum(attribute='tagged_instances', start=[]) }}