HowTo transform variable groups.my_group into array of dicts

Hi, I have a group named “zookeeper_node”:

tasks:

  • debug: msg=“System {{ groups.zookeeper_node }}”

OUTPUT:

ok: [mesosnode01] => {
“msg”: “System [‘mesosnode01’, ‘mesosnode02’, ‘mesosnode03’]”
}

I want to transform it into array of dicts:

[{‘host’: ‘mesosnode01’},{‘host’: ‘mesosnode02’}, {‘host’: ‘mesosnode03’}]

and pass as variable to role:

roles:

  • { role: ‘ansible-zookeeper’,
    zookeeper_hosts: “{{ groups.zookeeper_node | some magic expression here … }}”
    }

Can I do it in Ansible?

Did it using ugly filter:

def array_to_array_of_dicts(some_array, *args, **kw):
    data = []
    for some_value in some_array:
        data.append({'host': some_value})
    return data

class FilterModule (object):
    def filters(self):
        return {"array_to_array_of_dicts": array_to_array_of_dicts}