Score:1

Ansible populating a variable based on a json param with a json param

cn flag
raw

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.

Score:1
br flag

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
cn flag
raw
Thanks alot for your help !
br flag
Don't forget to [accept](https://stackoverflow.com/help/someone-answers) the answer if this is what you want.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.