{% for role in elasticsearch_users.values() | map(attribute="roles") | flatten | unique %} {{ role }}:{{ elasticsearch_users.items() | selectattr("1.roles", "contains", role) | map(attribute=0) | join(",") }} {% endfor %}