Working with registered results

95 views
Skip to first unread message

madd...@gmail.com

unread,
Jul 26, 2021, 11:03:00 AM7/26/21
to Ansible Project
Hello all,  I have a group of switches that users keep asking me to locate mac address for in order to trace down the exact port and edit that port to a new vlan. Rather than logging into each switch and tracking down the switch where the mac resides, I created an ansible a basic ansible playbook to help me with this:

---
- name: Find mac address in sec-switches
  hosts: sec-switch
  gather_facts: false
  connection: local
  vars_prompt:
     - name: mac
       prompt: What is the mac address?
       private: no
  tasks:
    -
      name: debugging
      ansible.builtin.debug:
        msg: 'Searching for {{ mac }}'
    -
      name: "search"
      register: output
      ios_command:
        commands:
          - "show mac address-table | include {{ mac }}"
    -
      debug: var=output.stdout_lines


When ran, my debug gives me:
(snippet)

}
ok: [10.1.1.32] => {
    "output.stdout_lines": [
        [
            "24    0050.f967.5cb7    DYNAMIC     Gi1/0/48"
        ]
    ]
}
ok: [10.1.1.33] => {
    "output.stdout_lines": [
        [
            "24    0050.f967.5cb7    DYNAMIC     Gi1/0/48"
        ]
    ]
}
ok: [10.1.1.30] => {
    "output.stdout_lines": [
        [
            "4    0050.f967.5cb7    DYNAMIC     Gi1/1/1",
            " 24    0050.f967.5cb7    DYNAMIC     Gi1/1/1"
        ]
    ]
}

I'd like to filter the output and for each switch run the sh interfaces description | inc { using the returned interfaces}. This way I can rule out uplinks.

IE:  On switch 10.1.1.33 I ran: sh interfaces description | inc Gi1/0/48
and it returned: Gi1/0/48                       up             up       UPLINK
 
That lets me know I don't need to worry about that switch as its an uplink.


I tried seeing if my returned output was a dictionary, but the playbook complained when I added {{ myvar | type_debug }} under my debug statement as: {{ output | type_debug }}

Thanks in advance.

madd...@gmail.com

unread,
Jul 26, 2021, 4:43:42 PM7/26/21
to Ansible Project
I'm getting closer using regex...trying to tweak the regex now (Still any help would be appreciated):

---
- name: Find mac address in sec-switches
  hosts: sec-switch
  gather_facts: false
  connection: local
  vars_prompt:
     - name: mac
       prompt: What is the mac address?
       private: no
  tasks:
    -
      name: debugging
      ansible.builtin.debug:
        msg: 'Searching for {{ mac }}'
    -
      name: "search"
      register: output
      ios_command:
        commands:
          - "show mac address-table | include {{ mac }}"
      register: printout
    - set_fact:
        intf: |
          {{printout.stdout_lines[0] |
            map('regex_replace','^(?:[^ ]*\ ){12}([^ ]*)') |
            list }}
    - name: View output
      debug:
        var: intf


Here is my output:
 ________________________
< TASK [View output] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [10.1.1.38] => {
    "intf": [
        " Gi1/0/49"
    ]
}

I still need to get rid of: [ and whitespace


Rajthecomputerguy

unread,
Jul 26, 2021, 10:00:02 PM7/26/21
to ansible...@googlegroups.com
Use strip() method to get rid of whitespace

      debug:
        var: intf[0].strip()

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e7acbf4a-ec9f-4df8-9cb4-c97de725a208n%40googlegroups.com.


--

Thanks,

Pushparaj G


madd...@gmail.com

unread,
Jul 26, 2021, 11:15:01 PM7/26/21
to Ansible Project
Thanks  Pushparaj, I'll give that a try & let you know

madd...@gmail.com

unread,
Jul 26, 2021, 11:24:20 PM7/26/21
to Ansible Project
That worked! Thank you very much  Pushparaj, you are the man!

madd...@gmail.com

unread,
Jul 27, 2021, 10:12:35 AM7/27/21
to Ansible Project
To take it one step further, in the output. I'd like to remove any results that have "UPLINK" in them

I have tried the following below, but "{{ intf_corrected2 }}" doesn't show me what  I'm expecting.
I'm expecting to see just the one switch which has the mac and the port isn't an uplink. Below is where I'm at:

---
- name: Find mac address in sec-switches
  hosts: sec-switch
  gather_facts: false
  connection: local
  vars_prompt:
     - name: mac
       prompt: What is the mac address?
       private: no
  tasks:
    -
      name: debugging
      ansible.builtin.debug:
        msg: 'Searching for {{ mac }}'
    -
      name: search
      ios_command:
        commands:
          - "show mac address-table | include {{ mac }}"
      register: printout
    - set_fact:
        intf: |
          {{printout.stdout_lines[0] |
            map('regex_replace','^(?:[^ ]*\ ){12}([^ ]*)') |
            list }}
    - set_fact:
        intf_corrected1={{ intf[0].strip() }}
    - debug:
        msg: "{{ intf_corrected1 }}"
    -
      name: show int desc
      ios_command:
        commands:
          - "sh interfaces description | inc {{ intf_corrected1 }}"
      register: printout2
    - name: View output
      debug:
        var: printout2
    - set_fact:
        intf_corrected2: |
          {{printout2 |
            map('regex_replace','^((?!UPLINK|uplink).)*$') |
            list }}
    - name: View output
      debug:
        msg: "{{ intf_corrected2 }}"
Reply all
Reply to author
Forward
0 new messages