Home > Networking > Automating Cisco Switch Config Backups with Ansible

Automating Cisco Switch Config Backups with Ansible

Network server room

Image: Managing network gear like Cisco switches gets easier with automation. (Source: Unsplash)

Why Automate Switch Backups?

Backing up Cisco switch configurations manually is tedious—logging in, running commands, saving files. What if you could automate it? Enter Ansible: an open-source tool that simplifies network tasks. In this post, we’ll walk through a simple demo to auto-backup a Cisco switch config using Ansible. It’s fast, repeatable, and a great intro to network automation. We’ll connect to a switch, grab its running config, and save it locally—here’s how.

What You’ll Need

  • Ansible Installed: Version 2.9+ on a Linux control node (e.g., Ubuntu).
  • Cisco Switch: Running IOS, with SSH enabled.
  • Credentials: Switch IP, username, and password.
  • Python Packages: Install ansible and pyats (pip install ansible pyats).

We’ll assume a switch at 192.168.1.10—adjust for your setup.

Step 1: Set Up Your Ansible Environment

First, create a project directory:


mkdir ansible-cisco-backup
cd ansible-cisco-backup

Next, define your switch in an inventory file (hosts.yml):


---
all:
  hosts:
    cisco_switch:
      ansible_host: 192.168.1.10
      ansible_user: admin
      ansible_password: cisco123
      ansible_network_os: ios
      ansible_connection: network_cli

Note: Store passwords securely in production—use Ansible Vault (ansible-vault encrypt hosts.yml) instead of plaintext.

Coding on laptop

Image: Writing Ansible playbooks is your gateway to automation. (Source: Pexels)

Step 2: Write the Backup Playbook

Create a playbook (backup_switch.yml) to fetch and save the config:


---
- name: Backup Cisco Switch Configuration
  hosts: cisco_switch
  gather_facts: no
  tasks:
    - name: Retrieve running-config
      ansible.netcommon.cli_command:
        command: show running-config
      register: config_output

    - name: Save config to file
      ansible.builtin.copy:
        content: "{{ config_output.stdout }}"
        dest: "backups/{{ inventory_hostname }}_config_{{ ansible_date_time.iso8601_basic_short }}.txt"

What’s Happening:

  • cli_command: Runs show running-config on the switch.
  • register: Stores the output in config_output.
  • copy: Saves it to a file in a backups folder, timestamped (e.g., cisco_switch_config_20250307T123456.txt).

Step 3: Run the Playbook

Create a backups directory:


mkdir backups

Then execute:


ansible-playbook -i hosts.yml backup_switch.yml

If successful, you’ll see output like:


PLAY [Backup Cisco Switch Configuration] ***************************************
TASK [Retrieve running-config] ************************************************
ok: [cisco_switch]
TASK [Save config to file] ****************************************************
changed: [cisco_switch]
PLAY RECAP ********************************************************************
cisco_switch : ok=2    changed=1    unreachable=0    failed=0

Check the backups folder for your config file!

Step 4: Verify the Backup

Peek at the saved file:


cat backups/cisco_switch_config_*.txt

You’ll see the full running-config—interfaces, VLANs, everything. Now you’ve got a timestamped backup, ready for restores or audits.

Network cables and gear

Image: Automating backups keeps your network configs safe and sound. (Source: Unsplash)

Why It’s Worth It

This demo scratches the surface—Ansible can scale to dozens of switches, schedule backups with cron, or push configs back. It’s less error-prone than manual work and saves time. Musk might dream big with Tesla, but automating your Cisco gear is a practical win you can claim today. Try tweaking the playbook—add more switches or commands—and see where it takes you!

Got questions or ideas to expand this? Share below!

Leave a Comment