1

I have a variable containing json:

{
    "ansible_facts": {
        "ansible_network_resources": {
            "interfaces": [
                {
                    "description": "*** - LOCAL A - ***",
                    "enabled": true,
                    "name": "FastEthernet0"
                },
                {
                    "description": "*** - LOCAL B - ***",
                    "enabled": true,
                    "name": "GigabitEthernet1/0/1"
                },
                {
                    "description": "*** - LOCAL C - ***",
                    "enabled": true,
                    "name": "FastEthernet1"
                }
            ]
        }
    }
}

And I need to populate a variable with the name of the interface when the description contains a certain word.

raw
  • 91
  • 2
  • 7

1 Answers1

1

Q: "Populate a variable with the name of the interface when the description contains a certain word."

A: For example

    - set_fact:
        result: "{{ ansible_facts.ansible_network_resources.interfaces|
                   selectattr('description', 'search', pattern)|
                   map(attribute='name')|
                   list }}"
      vars:
        pattern: "LOCAL A"

gives

  result:
  - FastEthernet0

and

    - set_fact:
        result: "{{ ansible_facts.ansible_network_resources.interfaces|
                    selectattr('description', 'search', pattern)|
                    map(attribute='name')|
                    list }}"
      vars:
        pattern: "LOCAL"

gives

  result:
  - FastEthernet0
  - GigabitEthernet1/0/1
  - FastEthernet1
Vladimir Botka
  • 5,138
  • 8
  • 20