Home > database >  ansible change value in list in nested dictionary
ansible change value in list in nested dictionary

Time:11-27

I would like to change addressType inside interface but the addressType is in the list.

Here is example of json format.

{
    "IPcontrol_static_dns": {
        "addressType": "static",
        "aliases": [],
        "hostname": "dc",
        "id": 872084447,
        "interfaces": [
            {
                "addressType": [
                    "Reserved"
                ],
                "excludeFromDiscovery": "false",
                "id": 872084447,
                "ipAddress": [
                    "2.82.50.34"
                ]
            }
        ],
        
        "resourceRecordFlag": "true"
    }
}

this is my playbook


- name: change dns record to reserve and add value
  set_fact:
    IPcontrol_static_dns: "{{ check_domain_name.json | combine({'addressType' : 'static' }, { 'resourceRecordFlag': 'true' },  recursive=True) }} "

- name: change dns record to reserve and add value continue
  set_fact:
    IPcontrol_static_dns: "{{ IPcontrol_static_dns | combine( { 'interfaces': [{ 'addressType': ['static'] }] },  recursive=True) }} "

Problem is that everything was delete inside the interface and just "addresType" left.

I would like just modify this value

part of result

 "id": 872084447,
        "interfaces": [
            {
                "addressType": [
                    "static"
                ]
            }
        ],
        "ipAddress": "2.82.50.34",
        "resourceRecordFlag": "true"

CodePudding user response:

this playbook does the job:

- name: test
  hosts: localhost
  vars:
    IP: "{{ lookup('file','filetest.json') | from_json }}"
  tasks:
    - name: set my variable
      debug:
        msg: "{{ IP }}"

    - name: change json
      set_fact:
        IP: "{{ IP | combine(dict1, recursive=true) }}"   
      vars:
        dict1: "{{ IP | combine(interface) }}"
        ipadr: "{{ IP.IPcontrol_static_dns.interfaces.0.ipAddress.0}}"
        interface:  { "IPcontrol_static_dns": {"interfaces": [{ "addressType": ["static"]}], 'ipAddress': "{{ipadr}}" }}

      
    - name: result
      debug:
        msg: "{{ IP }}"

result:

starting IP:

{
    "IPcontrol_static_dns": {
        "addressType": "static",
        "aliases": [],
        "hostname": "dc",
        "id": 872084447,
        "interfaces": [
            {
                "addressType": [
                    "Reserved"
                ],
                "excludeFromDiscovery": "false",
                "id": 872084447,
                "ipAddress": [
                    "2.82.50.34"
                ]
            }
        ],
        
        "resourceRecordFlag": "true"
    }
}

final IP:

ok: [localhost] => {
    "msg": {
        "IPcontrol_static_dns": {
            "addressType": "static",
            "aliases": [],
            "hostname": "dc",
            "id": 872084447,
            "interfaces": [
                {
                    "addressType": [
                        "static"
                    ]
                }
            ],
            "ipAddress": "2.82.50.34",
            "resourceRecordFlag": "true"
        }
    }
}
  • Related