Using Ansible set_fact to create a dictionary from register results


In Ansible I’ve used register to save the results of a task in the variable people. Omitting the stuff I don’t need, it has this structure:

I’d like to use a subsequent set_fact task to generate a new variable with a dictionary like this:

I guess this might be possible but I’m going round in circles with no luck so far.


I think I got there in the end.

The task is like this:

It loops through each of the dicts (item) in the people.results array, each time creating a new dict like {Bob: "male"}, and combine()s that new dict in the genders array, which ends up like:

It assumes the keys (the name in this case) will be unique.

I then realised I actually wanted a list of dictionaries, as it seems much easier to loop through using with_items:

This keeps combining the existing list with a list containing a single dict. We end up with a genders array like this:

Leave a Reply