02-21-2021 07:50 AM - last edited on 05-08-2023 10:32 AM by Paul Zimmerman
When is ansible useful? for network engineer
1. You know the server name - how to find the MAC address and port of the switch in one click
2. Add a new Vlan to a switch group in one click
3. Add a new Vlan to a group of ports in one click
1. You know the name of the server - how to find the MAC address and port of the switch in one click
this problem and solution has already been described by Hank Preston https://blogs.cisco.com/developer/ask-hank-how-can-i-automate-a-mac-address-interface-report
his solution relies on Cisco pyATS and my opinion is slightly complex. I suggest a little easier.
first, you need to create a folder with files containing the result of the show arp and show mac address-table commands. the file name must match the device name (these are all L2 and L3 switches)
further, if we work in Windows, we will have to use the dos window and findstr command twice, the first time to find the IP address and the second time to find the mac address. in linux you can in one click grep inside another grep and at the output we will get the name of the switch and the port number.
how to create a folder? there are many methods like bash python ansible
if there is interest, then I will continue this topic in detail. please express your opinions
03-02-2021 10:43 AM
here is example ansible playbook to create sh arp and sh mac address table files:
---
-
name: Backup Configs
hosts: all
gather_facts: no
tasks:
- name: Generate Backup Configs
cisco.ios.ios_config:
backup: yes
backup_options:
dir_path: ./config/
filename: "{{inventory_hostname}}.config"
- name: sh arp
ios_command:
commands: show arp
register: output
- name: output
debug:
var: output.stdout
- name: copy to file
copy: content="{{ output.stdout[0] }}" dest=./arp/{{ inventory_hostname }}.txt
- name: sh mac-add
ios_command:
commands: show mac add
register: output
- name: output
debug:
var: output.stdout
- name: copy to file
copy: content="{{ output.stdout[0] }}" dest=./mac/{{ inventory_hostname }}.txt
===========
here is example bash script:
tagir@ubuntu:~$ more sh_arp-mac.bash
#!/usr/bin/bash
##
##
##
newstr=`grep -H $1 ./arp/*`
echo $newstr
newfac=$(echo $newstr | awk '{ print $4 }')
echo $newfac
grep -H $newfac ./mac/*
here is result of bash script:
10-21-2022 12:56 PM
Hello @Tagir Temirgaliyev,
I may or may not try out your Ansible script, but I am definitely going to follow-up by building some pyATS automation using the ideas you've laid out in your post above. I also plan to add a few twists, based upon my network environment, as follows:
I'll start the project by:
Once I'm confident the BASH / AWK scripts will work, I'll need to:
Thanks for your post!!
It may take awhile, but hopefully your post will lead to a great troubleshooting tool for my network!!
@ittybittypacket
fjm / vrs
11-05-2022 01:08 PM - edited 11-05-2022 01:14 PM
How to add a new Vlans to a group of ports in one click using ansible and keep configuration consistency
for demonstration purpose, I created a simplified topology with 2 switches and 4 servers.
two servers belong to cluster A, and two more servers belong to cluster B. All ports trunk.
Of course, in reality, we meet with a much more complex topology where there can be up to 100 or more servers and up to 100 or more switches.
in a real production environment, tasks appear to add or remove one or more vlans to cluster A or cluster B, or to add or remove one or more servers to cluster and you have to add vlan manually to hundreds of ports.
in reality, configuration consistency is very important.
using ansible, you can add new vlan (or remove) to the list of vlans in the cluster in only one place.
here is a simple ansible playbook screenshot
here switches before adding vlans
switches after adding vlans
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: