LJS
LJS

Reputation: 327

Ansible - convert simple list to a simple dict

I have the following list:

    - hosts: localhost
      gather_facts: false
    
      vars:
        list1: [1,2,3,4]  

      tasks:

      

I need this to be of a type dict with key-value pairs where each member of list (1,2,3,4,...n) will have key "a".

dict1: [{a: 1}, {a:2}, {a:3}, {a:4}]

I know there is list2dict filter, but couldn't find examples or literature that would explain it how to do it. PS there can be n number of items in the list.

Thanks!

Upvotes: 1

Views: 330

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68189

See filter community.general.dict_kv. For example,

dict1: "{{ list1|map('community.general.dict_kv', 'a') }}"

gives what you want

dict1:
  - a: 1
  - a: 2
  - a: 3
  - a: 4

Upvotes: 2

Related Questions