Thanks Todd. Honestly I came right off the dict module page that has an almost 
exact example.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Aug 29, 2024, at 8:34 AM, Todd Lewis <[email protected]> wrote:

That is elegant. Much better than the abomination I came up with:

---
# dick-visser-03.yml
- name: Add helper keys to list of dicts
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    test:
      - - host1
        - fd99:9999::1
      - - host1
        - 10.0.1.1
      - - host2
        - fd99:9999::1
      - - host2
        - 10.0.1.1
      - - foo
        - fd99:9999::1
      - - foo
        - 10.0.1.1
      - - baz
        - fd99:9999::1
      - - baz
        - 10.0.1.1
  tasks:
    - name: Dictify the test data - 1
      ansible.builtin.debug:
        msg:
          - 'test'
          - '{{ test }}'

          - '   test | map("map","community.general.dict_kv","value")'
          - '{{ test | map("map","community.general.dict_kv","value") }}'

          - '   test | map("map","community.general.dict_kv","value") | 
map("zip",[{"key":"name"},{"key":"address"}])'
          - '{{ test | map("map","community.general.dict_kv","value") | 
map("zip",[{"key":"name"},{"key":"address"}]) }}'

          - '   test
                | map("map","community.general.dict_kv","value")
                | map("zip",[{"key":"name"},{"key":"address"}])
                | map("map","flatten",1)
                | map("map","combine")'
          - '{{ test
                | map("map","community.general.dict_kv","value")
                | map("zip",[{"key":"name"},{"key":"address"}])
                | map("map","flatten",1)
                | map("map","combine") }}'

          - '   test
                | map("map","community.general.dict_kv","value")
                | map("zip",[{"key":"name"},{"key":"address"}])
                | map("map","flatten",1)
                | map("map","combine")
                | map("items2dict")'
          - '{{ test
                | map("map","community.general.dict_kv","value")
                | map("zip",[{"key":"name"},{"key":"address"}])
                | map("map","flatten",1)
                | map("map","combine")
                | map("items2dict") }}'


On 8/29/24 7:38 AM, 'Rowe, Walter P. (Fed)' via Ansible Project wrote:
https://docs.ansible.com/ansible/latest/collections/community/general/dict_filter.html

This is an even more elegant solution using the community.general.dict module:


    - name: list2dict
      set_fact:
        list2dict: >-
          {{ test | map('zip', ['name', 'addr'])
                | map('map', 'reverse')
                | map('community.general.dict') }}

    - name: list2dict
      debug: var=list2dict



Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Aug 29, 2024, at 7:22 AM, Rowe, Walter P. (Fed) 
<[email protected]><mailto:[email protected]> wrote:



- name: list of list to list of dict
  hosts: localhost
  become: false
  gather_facts: false
  vars:
    test:
      - - host1
        - fd99:9999::1
      - - host1
        - 10.0.1.1
      - - host2
        - fd99:9999::1
      - - host2
        - 10.0.1.1
      - - foo
        - fd99:9999::1
      - - foo
        - 10.0.1.1
      - - baz
        - fd99:9999::1
      - - baz
        - 10.0.1.1
  tasks:
    - name: test
      debug: var=test

    - name: list 2 dict
      set_fact:
        list2dict: "{{ list2dict|default([]) + [ thisdict ] }}"
      loop: "{{ test }}"
      vars:
        thisdict: { name: "{{ item.0 }}", addr: "{{ item.1 }}" }

    - name: list2dict
      debug: var=list2dict




Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Aug 29, 2024, at 5:40 AM, Dick Visser 
<[email protected]><mailto:[email protected]> wrote:

Hii

I have something like this in my vars:

test:
  - - host1
    - fd99:9999::1
  - - host1
    - 10.0.1.1
  - - host2
    - fd99:9999::1
  - - host2
    - 10.0.1.1
  - - foo
    - fd99:9999::1
  - - foo
    - 10.0.1.1
  - - baz
    - fd99:9999::1
  - - baz
    - 10.0.1.1


Each first and second list item should become keys in a dict, like this:

conv:
  - name: host1
    address: fd99:9999::1
  - name: host1
    address: 10.0.1.1
  - name: host2
    address: fd99:9999::1
  - name: host2
    address: 10.0.1.1
  - name: foo
    address: fd99:9999::1
  - name: foo
    address: 10.0.1.1
  - name: baz
    address: fd99:9999::1
  - name: baz
    address: 10.0.1.1

I tried a few things with mapping and filtering, but can't see how to do it....
any suggestions? Ideally no tasks, just filters....

thx




--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAF8BbLamT%3Ds3H4R%3DcxShSg16KrSr%2B4%3D%3DUytgZbjPivhMK1rfmg%40mail.gmail.com<https://groups.google.com/d/msgid/ansible-project/CAF8BbLamT%3Ds3H4R%3DcxShSg16KrSr%2B4%3D%3DUytgZbjPivhMK1rfmg%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6F5FBEE5-028A-4AB2-ADC9-5B2B535290F7%40nist.gov<https://groups.google.com/d/msgid/ansible-project/6F5FBEE5-028A-4AB2-ADC9-5B2B535290F7%40nist.gov?utm_medium=email&utm_source=footer>.


--
Todd

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0d6ba760-b60c-4749-8fa6-8ccd623c0959%40gmail.com<https://groups.google.com/d/msgid/ansible-project/0d6ba760-b60c-4749-8fa6-8ccd623c0959%40gmail.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/22D3870A-F3A0-4A4F-B18D-0D480EB53634%40nist.gov.

Reply via email to