Very simple and easy Ansible playbook for Cisco devices to save output show command as a file.
In this example I show you how you can save “show mac address-table”, on many Cisco devices as you want.
Imagine that you have been given a task where a large company wants from you to login on each switches at 2000 locations and run “show mac address-table” and save outputs as a text file.
You will probably not login to every single switches one by one to complete the task.
If you spend 2-3 minutes on per location, this will be 6-9K minutes to complete it.
Use my solution and use these saved times to develop yourself or spend it with those you love!
For pretty output please be sure that you have already installed humman_log
Let’s get started.- Create an empty YAML file and name it mac_address_table.yaml with below codes:
# @author [Hawar Koyi] # @email [hawar.koy@gmail.com] # @create date 2021-22-09 18:22 # @desc [Network Automation - Save show command on Cisco] --- - name: Get port details and MAC-addresses hosts: Cisco gather_facts: false connection: local tasks: - name: Accespt ansible net hostname ios_facts: gather_subset: all - name: Save your show command to register ios_command: commands: 'show mac address-table dynamic' register: output #Save contents to disk - name: Copy output to file copy: content="{{ output.stdout | replace('\\n', '\n') }}" # #Change destination/direcotry dest=/home/hawar/mac_add/{{ ansible_net_hostname }}_{{ inventory_hostname }}.txt #End of codes
2. Create an empty text file and name it hosts with devices IP-addresses and the defaults values.
As you can see I only added one device, otherwise you can add more than one device, if they have the same username and password.[Cisco] 192.168.10.123 192.168.xx.xx [Cisco:vars] ansible_connection=network_cli ansible_network_os=ios ansible_user=cisco ansible_password=cisco ansible_become=yes ansible_become_method=enable ansible_become_password=cisco
ansible_password: Device SSH password
ansible_become_password: Device enable password3. Run this command:
ansible-playbook -i hosts mac_address_table.yaml
Saved file looks like the image below.