Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Parsing JSON outputs

Variables required

Dependencies

Install ansible.utils.

ansible-galaxy collection install ansible.utils

Inputs

JSON file ospf.json.

{ 
    "parsed": {
       "interfaces": {
           "Tunnel0": {
               "neighbors": {
                   "203.0.113.2": {
                       "address": "198.51.100.2",
                       "dead_time": "00:00:39",
                       "priority": 0,
                       "state": "FULL/  -"
                   }
               }
           },
           "Tunnel1": {
               "neighbors": {
                   "203.0.113.2": {
                       "address": "192.0.2.2",
                       "dead_time": "00:00:36",
                       "priority": 0,
                       "state": "INIT/  -"
                   }
               }
           }
       }
    }
}

Playbook

Latest version -> test-json. The following output might be outdated.

- name: Play around with JSON inputs
  hosts: localhost
  connection: local
  become: false
  gather_facts: false
  vars:
    input: "{{ lookup('file','files/ospf.json') | from_json }}"

  tasks:
    - name: Create interfaces Dictionary
      set_fact:
        interfaces: "{{ input.parsed.interfaces }}"
  
    - name: Print out flatten interfaces input
      debug:
        msg:  "{{ lookup('ansible.utils.to_paths', interfaces) }}"

    - name: TEST 1
      block:
      - name: Loop over interfaces
        include_tasks: test-json-tasks-1.yml
        with_items: "{{ interfaces | dict2items }}"

    - name: TEST 2
      block:
      - name: Create neighbors dictionary (this is now per interface)
        set_fact:
          neighbors: "{{ interfaces | json_query('*.neighbors') }}"

      - name: Loop over neighbors
        include_tasks: test-json-tasks-2.yml
        with_items: "{{ neighbors }}"
        loop_control:
          loop_var: data

    - name: TEST 3
      block:
      - name: Loop over neighbors
        include_tasks: test-json-tasks-3.yml
        with_items: "{{ neighbors }}"
        loop_control:
          loop_var: data

    - name: TEST 4
      block:
      - name: Loop with deep json_query
        debug:
          var: "{{ item }}"
        with_items: "{{ input | json_query('parsed.interfaces.*.neighbors[].*.[address, state]') }}"

    - name: TEST 5
      block:
      - name: Loop over neighbors and validate data with a schema
        include_tasks: test-json-tasks-4.yml
        with_items: "{{ neighbors }}"
        loop_control:
          loop_var: data

Output

The following output might be outdated.

⇨  ansible-playbook test-json.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Play around with JSON inputs] ***********************************************************************************************************************

TASK [Create interfaces Dictionary] ***********************************************************************************************************************
ok: [localhost]

TASK [Print out flatten interfaces input] *****************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "Tunnel0.neighbors['203.0.113.2'].address": "198.51.100.2",
        "Tunnel0.neighbors['203.0.113.2'].dead_time": "00:00:39",
        "Tunnel0.neighbors['203.0.113.2'].priority": 0,
        "Tunnel0.neighbors['203.0.113.2'].state": "FULL/  -",
        "Tunnel1.neighbors['203.0.113.2'].address": "192.0.2.2",
        "Tunnel1.neighbors['203.0.113.2'].dead_time": "00:00:36",
        "Tunnel1.neighbors['203.0.113.2'].priority": 0,
        "Tunnel1.neighbors['203.0.113.2'].state": "INIT/  -"
    }
}

TASK [Loop over interfaces] *******************************************************************************************************************************
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-1.yml for localhost
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-1.yml for localhost

TASK [Print out neighbor data - FORMAT 1] *****************************************************************************************************************
ok: [localhost] => {
    "msg": "Neighbor: 203.0.113.2, with address: 198.51.100.2 -> State: FULL"
}

TASK [Print out neighbor data - FORMAT 1] *****************************************************************************************************************
ok: [localhost] => {
    "msg": "Neighbor: 203.0.113.2, with address: 192.0.2.2 -> State: INIT"
}

TASK [Create neighbors dictionary (this is now per interface)] ********************************************************************************************
ok: [localhost]

TASK [Loop over neighbors] ********************************************************************************************************************************
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-2.yml for localhost
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-2.yml for localhost

TASK [Print out neighbor data - FORMAT 2] *****************************************************************************************************************
ok: [localhost] => {
    "msg": "Neighbor: 203.0.113.2, with address: 198.51.100.2 -> State: FULL"
}

TASK [Print out neighbor data - FORMAT 2] *****************************************************************************************************************
ok: [localhost] => {
    "msg": "Neighbor: 203.0.113.2, with address: 192.0.2.2 -> State: INIT"
}

TASK [Loop over neighbors] ********************************************************************************************************************************
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-3.yml for localhost
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-3.yml for localhost

TASK [Print out a WARNING if OSPF state is not FULL] ******************************************************************************************************
skipping: [localhost]

TASK [Print out a WARNING if OSPF state is not FULL] ******************************************************************************************************
ok: [localhost] => {
    "msg": "WARNING: Neighbor 203.0.113.2, with address 192.0.2.2 is in state INIT"
}

TASK [Loop with deep json_query] **************************************************************************************************************************
ok: [localhost] => (item=['198.51.100.2', 'FULL/  -']) => {
    "<class 'list'>": "VARIABLE IS NOT DEFINED!",
    "ansible_loop_var": "item",
    "item": [
        "198.51.100.2",
        "FULL/  -"
    ]
}
ok: [localhost] => (item=['192.0.2.2', 'INIT/  -']) => {
    "<class 'list'>": "VARIABLE IS NOT DEFINED!",
    "ansible_loop_var": "item",
    "item": [
        "192.0.2.2",
        "INIT/  -"
    ]
}

TASK [Loop over neighbors and validate data with a schema] ************************************************************************************************
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-4.yml for localhost
included: /home/nleiva/Ansible/ansible-networking/test-json-tasks-4.yml for localhost

TASK [Validate state of the neighbor is FULL] *************************************************************************************************************
ok: [localhost]

TASK [Print the neighbor that does not satisfy the desired state] *****************************************************************************************
skipping: [localhost]

TASK [Validate state of the neighbor is FULL] *************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "errors": [{"data_path": "203.0.113.2.state", "expected": "^FULL", "found": "INIT/  -", "json_path": "$.203.0.113.2.state", "message": "'INIT/  -' does not match '^FULL'", "relative_schema": {"pattern": "^FULL", "type": "string"}, "schema_path": "patternProperties..*.properties.state.pattern", "validator": "pattern"}], "msg": "Validation errors were found.\nAt 'patternProperties..*.properties.state.pattern' 'INIT/  -' does not match '^FULL'. "}
...ignoring

TASK [Print the neighbor that does not satisfy the desired state] *****************************************************************************************
ok: [localhost] => {
    "msg": [
        "WARNING: Neighbor 203.0.113.2, with address 192.0.2.2 is in state INIT",
        "203.0.113.2.state, found: INIT/  -, expected: ^FULL"
    ]
}

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