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 }}"




On Monday, July 26, 2021 at 11:24:20 PM UTC-4 [email protected] wrote:

> That worked! 
> *Thank you very much  Pushparaj, you are the man!*
> On Monday, July 26, 2021 at 11:15:01 PM UTC-4 [email protected] wrote:
>
>> Thanks  Pushparaj, I'll give that a try & let you know
>>
>> On Monday, July 26, 2021 at 10:00:02 PM UTC-4 rajthecomputerguy wrote:
>>
>>> Use strip() method to get rid of whitespace
>>>
>>>       debug:
>>>         var: intf[0].strip()
>>>
>>> On Tue, Jul 27, 2021 at 2:13 AM [email protected] <[email protected]> 
>>> wrote:
>>>
>>>> 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
>>>>
>>>>
>>>> On Monday, July 26, 2021 at 11:03:00 AM UTC-4 [email protected] wrote:
>>>>
>>>>> 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.
>>>>>
>>>>> Any help would be appreciated. I looked into 
>>>>> https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html
>>>>>
>>>>> 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.
>>>>>
>>>>> -- 
>>>> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/ansible-project/e7acbf4a-ec9f-4df8-9cb4-c97de725a208n%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/ansible-project/e7acbf4a-ec9f-4df8-9cb4-c97de725a208n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>
>>>
>>> -- 
>>>
>>> Thanks,
>>>
>>> Pushparaj G
>>>
>>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0581d25d-75bf-41b8-8422-c435c7692a62n%40googlegroups.com.

Reply via email to