0

I have a dictionary like this:

list_of_dicts:
  config:
    key1: value1
    key2: value2

When I loop the dictionary I want to combine key/value pair.

set_facts:
  list_of_dicts: {{ list_of_dicts | default({}) | combine (item.key: item.value) }}
with_dict: "{{ list_of_dicts }}"

How can I get the value of item.value.key ?

0

With the filter_plugin below (credit Coderwall)

> cat filter_plugins/hash_utils.py
def hash_to_tuples(h):
    return h.items()

def hash_keys(h):
    return h.keys()

def hash_values(h):
    return h.values()

class FilterModule(object):
    ''' utility filters for operating on hashes '''

    def filters(self):
        return {
            'hash_to_tuples' : hash_to_tuples
            ,'hash_keys'     : hash_keys
            ,'hash_values'   : hash_values
        }

The play

- debug:
    msg: "{{ item|hash_keys }}"
  loop:  "{{ list_of_dicts.values() }}"

gives (abridged):

    "msg": [
    "key2", 
    "key1"
]

You might want to try hash_to_tuples and hash_values filters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.