How to extract one key from list of dictionaries in Ansible

Lists may contain dictionaries. This article explains the steps to extract one key from list of dictionaries in Ansible.

Consider the following list of dictionaries containing the names of people. Please note that each dictionaries containing one key:value pair.

users=[{ "name": "test1" }, { "name": "test2" }, { "name": "test3" }]

In-order to get a single list of names, we can use the following function in Ansible.

{{ user | map(attribute='name') | list }}

The sample result will be like as follows.

[ "test1", "test2", "test3" ]

That's all…

Leave a Reply