Search code examples
ansibleansible-2.xansible-template

create ansible dict from two list with a condition


I have many strings like this stored in list one

set bgp x vlan y tags-vlan bgp z,
set bgp x1 vlan y1 tags-vlan bgp z1,
.
.
.
.

and a second list contain values like this:

f,
j,
h,
m

from the first list of strings, i want to trim it and create from it a dict that contain values in this form:

bgp: x
vlan:y
tags-vlan bgp:z
-----
(to be added) details: p

and then add to the dict a fourth key called "details" which it's value is equal to the third value "h" in the second list, but add this key to the dict only when each time j == z, meaning second element of second list must equal third key value in newly made dict z and both have numeric value then add h to dict with the key name details.

the result is a dictionary like this, with h value being added with condition whenver z == j

bgp: x
vlan:y
tags-vlan bgp:z
details: h

Solution

  • Given the strings

      s1: |-
        set bgp x vlan y tags-vlan bgp z,
        set bgp x1 vlan y1 tags-vlan bgp z1,
        set bgp x2 vlan y2 tags-vlan bgp j
      s2: |-
        f,
        j,
        h,
        m
    

    1. Split the lines
      l1: "{{ s1|split('\n')|join()|split(',') }}"
      l2: "{{ s2|split('\n')|join()|split(',') }}"
    

    gives

      l1:
      - set bgp x vlan y tags-vlan bgp z
      - set bgp x1 vlan y1 tags-vlan bgp z1
      - set bgp x2 vlan y2 tags-vlan bgp j
    
      l2:
      - f
      - j
      - h
      - m
    
    1. Using the condition

    If

     'the second item of the second list' == 'the value of the third key'
    

    add

     {'details':  'third item of the second list'}
    

    Create the structure

        s3: |
          {% for i in l1 %}
          {% set a=i.split(' ') %}
          - {{ a.1 }}: {{ a.2 }}
            {{ a.3 }}: {{ a.4 }}
            {{ a.5 }} {{ a.6 }}: {{ a.7 }}
          {% if a.7 == l2.1 %}
            details: {{ l2.2 }}
          {% endif %}
          {% endfor %}
    

    gives

      s3: |-
      - bgp: x
        tags-vlan bgp: z
        vlan: y
      - bgp: x1
        tags-vlan bgp: z1
        vlan: y1
      - bgp: x2
        details: h
        tags-vlan bgp: j
        vlan: y2
    
    1. Convert the string to yaml
      l3: "{{ s3|from_yaml }}"
    

    gives the list of the dictionaries

      l3:
      - bgp: x
        tags-vlan bgp: z
        vlan: y
      - bgp: x1
        tags-vlan bgp: z1
        vlan: y1
      - bgp: x2
        details: h
        tags-vlan bgp: j
        vlan: y2
    

    Example of a complete playbook for testing

    - hosts: localhost
    
      vars:
    
        s1: |-
          set bgp x vlan y tags-vlan bgp z,
          set bgp x1 vlan y1 tags-vlan bgp z1,
          set bgp x2 vlan y2 tags-vlan bgp j
        s2: |-
          f,
          j,
          h,
          m
    
        l1: "{{ s1|split('\n')|join()|split(',') }}"
        l2: "{{ s2|split('\n')|join()|split(',') }}"
        s3: |
          {% for i in l1 %}
          {% set a=i.split(' ') %}
          - {{ a.1 }}: {{ a.2 }}
            {{ a.3 }}: {{ a.4 }}
            {{ a.5 }} {{ a.6 }}: {{ a.7 }}
          {% if a.7 == l2.1 %}
            details: {{ l2.2 }}
          {% endif %}
          {% endfor %}
        l3: "{{ s3|from_yaml }}"
    
      tasks:
    
        - debug:
            var: l1
        - debug:
            var: l2
        - debug:
            var: s3
        - debug:
            var: l3