Home > Software design >  ansible: stdout to list and dictionary
ansible: stdout to list and dictionary

Time:10-19

below is output of the "RESULT":

stdout:
- |-
  ospf T1 VRF vrf1
  ospf T2 VRF vrf2
  ospf T3 VRF vrf3
stdout_lines:
  - ospf T1 VRF vrf1
  - ospf T2 VRF vrf2
  - ospf T3 VRF vrf3

I want output in list and in dictionary:

1st output will be list. list will have following:

 - T1
 - T2
 - T3

2nd output will be list like below:

ospf_vrf:
  - vrf: vrf1
    process: T1
  - vrf: vrf2
    process: T2
  - vrf: vrf3
    process: T3

3rd output will be dictionary.

how to do that?

CodePudding user response:

I have created this sample playbook for your requirements. I am not clear on third expected output. please clarify on that


- hosts: localhost
  gather_facts: no
  vars:
    stdout_lines:
      - ospf T1 VRF vrf1
      - ospf T2 VRF vrf2
      - ospf T3 VRF vrf3

  tasks:
    - set_fact:
        first_output: "{{ first_output | default([])    [ (item | split())[1] ] }}"

      with_items: 
        "{{ stdout_lines }}"

    - debug:
        var: first_output

    - set_fact:
          second_output: "{{ second_output | default([])    [ { 'vrf': (item | split())[3] , 'process': (item | split())[1] } ] }}"

      with_items: 
        "{{ stdout_lines }}"

    - debug:
        var: second_output

CodePudding user response:

I would start by parsing the data from stdout via regex. Assuming the components in your output are always: ospf <process_value> VRF <vrf_falue>, the regex looks like this:

{{ stdout | regex_findall('^ospf (. ) VRF (. )$', multiline=True) }}

Based on this, you can then put the values into different forms and store them in variables:

- set_fact:
    ospf: "{{ extracted | map('first') }}"
    ospf_vrf: "{{ extracted | map('zip', ['process', 'vrf']) |
      map('map', 'reverse') | map('community.general.dict') }}"
    ospf_dict: "{{ dict(extracted) }}"
    vrf_dict: "{{ dict(extracted | map('reverse')) }}"
  vars:
    extracted: "{{ stdout | regex_findall('^ospf (. ) VRF (. )$', multiline=True) }}"
  • extracted is a helper with the regex values and the base for all other values

  • ospf is a list of the process_values In each case the first element of the regex is taken.

  • ospf_vrf is a list of dicts

    1. the keys process and vrf are assigned to the regex values
    2. by reverse the keys are brought to the front
    3. by community.general.dict a dict is created from the key-value pairs
  • ospf_dict: a dict based on process

  • vrf_dict: a dict based on vrf

Note: Both ospf_dict and vrf_dict will lose values if there are duplicates for the keys.

The output of the 4 variables looks like this:

TASK [debug] ***************************
ok: [localhost] => {
    "ospf": [
        "T1",
        "T2",
        "T3"
    ]
}

TASK [debug] ***************************
ok: [localhost] => {
    "ospf_vrf": [
        {
            "process": "T1",
            "vrf": "vrf1"
        },
        {
            "process": "T2",
            "vrf": "vrf2"
        },
        {
            "process": "T3",
            "vrf": "vrf3"
        }
    ]
}

TASK [debug] ***************************
ok: [localhost] => {
    "ospf_dict": {
        "T1": "vrf1",
        "T2": "vrf2",
        "T3": "vrf3"
    }
}

TASK [debug] ***************************
ok: [localhost] => {
    "vrf_dict": {
        "vrf1": "T1",
        "vrf2": "T2",
        "vrf3": "T3"
    }
}
  • Related