Home > Net >  Rebuilt a list of dictionary
Rebuilt a list of dictionary

Time:11-06

I can't rebuild a list of dictionary in Ansible code as below:

Input:

{
    "list_report": [
        {
            "objectid": "8502d7d99a435532", 
            "value": "10.70.108.15,10.70.106.10,10.72.106.167"
        }
    ]
}

To have in output:

{
    "list_report": [
        {
            "objectid": "8502d7d99a435532", 
            "value": [10.70.108.15,10.70.106.10,10.72.106.167]
        }
    ]
}

So, I need to keep item: objectid as it is, but, I want to change item: value from string to a list of IPs. I tried a lot of solution but nothing seems to work.

Can someone give me a clue?

CodePudding user response:

Given that you did not over simplified your example input, you could just split the value using Python's split method.

Given the playbook:

- hosts: localhost
  gather_facts: no
  vars:
    list_report: 
      - objectid: "8502d7d99a435532"
        value: "10.70.108.15,10.70.106.10,10.72.106.167"

  tasks:
    - set_fact:
        list_report: "{{ 
          [{
            'objectid': list_report.0.objectid, 
            'value': list_report.0.value.split(',')
          }] 
        }}"
    
    - debug:
        var: list_report

This yields:

PLAY [localhost] *************************************************************************************************

TASK [set_fact] **************************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "list_report": [
        {
            "objectid": "8502d7d99a435532",
            "value": [
                "10.70.108.15",
                "10.70.106.10",
                "10.72.106.167"
            ]
        }
    ]
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Now, if you have more than one item in this array, you'll have to resort to a temporary variable, because, editing it in place won't be possible anymore.

Given the playbook:

- hosts: localhost
  gather_facts: no
  vars:
    list_report: 
      - objectid: "8502d7d99a435532"
        value: "10.70.108.15,10.70.106.10,10.72.106.167"
      - objectid: "8502d7d99a435533"
        value: "10.70.108.16,10.70.106.11,10.72.106.168"

  tasks:
    - set_fact:
        _list_report: "{{ 
          _list_report | default([])   
          [{
            'objectid': item.objectid,
            'value': item.value.split(',')
          }]
        }}"
      loop: "{{ list_report }}"
      loop_control:
        label: "{{ item.objectid }}"

    - set_fact:
        list_report: "{{ _list_report }}"
    
    - debug:
        var: list_report

This yields the reacp:

PLAY [localhost] *************************************************************************************************

TASK [set_fact] **************************************************************************************************
ok: [localhost] => (item=8502d7d99a435532)
ok: [localhost] => (item=8502d7d99a435533)

TASK [set_fact] **************************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************
ok: [localhost] => 
  list_report:
  - objectid: 8502d7d99a435532
    value:
    - 10.70.108.15
    - 10.70.106.10
    - 10.72.106.167
  - objectid: 8502d7d99a435533
    value:
    - 10.70.108.16
    - 10.70.106.11
    - 10.72.106.168

PLAY RECAP *******************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

CodePudding user response:

For example, given the list

    list_report:
      - objectid: "8502d7d99a435532"
        value: "10.70.108.15,10.70.106.10,10.72.106.167"
      - objectid: "8502d7d99a435533"
        value: "10.70.108.16,10.70.106.11,10.72.106.168"

the task below does the job

    - set_fact:
        list_report: "{{ dict(_objs|zip(_vals))|
                         dict2items(key_name='objectid', value_name='value') }}"
      vars:
        _objs: "{{ list_report|map(attribute='objectid')|list }}"
        _vals: "{{ list_report|map(attribute='value')|map('split', ',')|list }}"

gives

  list_report:
  - objectid: 8502d7d99a435532
    value:
    - 10.70.108.15
    - 10.70.106.10
    - 10.72.106.167
  - objectid: 8502d7d99a435533
    value:
    - 10.70.108.16
    - 10.70.106.11
    - 10.72.106.168
  • Related