Vinny
Vinny

Reputation: 316

How to dynamically take the matching value for the input string in Ansible

Here i have a working script which take the relevant values statically by mentioning in the script. I am looking for a solution to dynamically take the values that matches between input.yml file subnetname and rules.yml.

For eg: if there is a new subnet called net1 defined in the input.yml, then the playbook should be able to match the value and update the rules.yml dynamically and no change should be done to the playbook if there is a new subnetname addition in the input.yml.

Playbook

- set_fact:
        ruleoutput: >-
                {{
                ruleoutput | default([]) 
                  + [ item | combine({'destCidr': item.destCidr}) | combine({'netname': details.0.Destination})]
                }} 
      loop: "{{ rules | list}}"
      vars:
        po_id: >- 
            {{
              input |items2dict(key_name='subnetname',
                                              value_name='po_id')
            }}
        DNS: "{{ po_id.DNS | default([])}}"  
        NTP: "{{ po_id.NTP | default([])}}" 
        NET: "{{ po_id.NET | default([])}}"

Input.yml

[
    {
        
        "id": "11155588779966",
        "subnetname": "DNS"
        
    },
    {  
        "id": "99996688778855",
        "subnetname": "NTP"
    },
    {
        "id": "123456789101112",
        "subnetname": "NET"
        
    }
]

rules.yml

rules:
         -    rule number: "1" 
              destCidr: "OBJ({{DNS}}) , 10.22.22.0/24"
                  

         -    rule number: "2"
              destCidr: "OBJ({{ NTP }}) , 10.33.33.0/24"
              

         -    rule number: "3"
              destCidr: "OBJ({{ NET }}), GRP({{ NTP }}) , 10.33.33.0/24"

Expected Output

[
    {
        "rule number": "1",
        "destCidr": "OBJ(11155588779966), 10.22.22.0/24 "
    },
    {
        "rule number": "2",
        "destCidr": "OBJ(99996688778855), 10.33.33.0/24"
    },
    {    
        "rule number": "3",
        "destCidr": "OBJ(123456789101112), GRP(99996688778855), 10.33.33.0/24 "
    }
]

Upvotes: 0

Views: 142

Answers (1)

Frenchy
Frenchy

Reputation: 17027

try this solution:

- name: "make this working"
  hosts: localhost
  vars:
    input: "{{ lookup('file', './input.yml') | from_json }}"
    rules:
      - rule number: "1" 
        destCidr: "OBJ({{po_id.DNS}}) , 10.22.22.0/24"
      - rule number: "2"
        destCidr: "OBJ({{ po_id.NTP }}) , 10.33.33.0/24"
      - rule number: "3"
        destCidr: "OBJ({{ po_id.NET }}), GRP({{ NTP }}) , 10.33.33.0/24"
  tasks:
    - set_fact:
         po_id: >- 
            {{
              input |items2dict(key_name='subnetname',
                                              value_name='id')
            }}
    
    - set_fact:
        ruleoutpout: "{{ rules }}"

    - debug:
        var: ruleoutpout

result:

ok: [localhost] => {
    "ruleoutpout": [
        {
            "destCidr": "OBJ(11155588779966) , 10.22.22.0/24",
            "rule number": "1"
        },
        {
            "destCidr": "OBJ(99996688778855) , 10.33.33.0/24",
            "rule number": "2"
        },
        {
            "destCidr": "OBJ(123456789101112), GRP(99996688778855) , 10.33.33.0/24",
            "rule number": "3"
        }
    ]
}

Upvotes: 1

Related Questions