Home > Software design >  Ansible - convert simple list to a simple dict
Ansible - convert simple list to a simple dict

Time:10-07

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!

CodePudding user response:

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
  • Related