New playbook to backup Cisco Small Business switches with Ansible

I backup my network device configurations with Ansible, as you can read in the previous post. Recently the backup of Cisco Small Business switches started to consistently fail. Maybe related to an update of the firmware. An internet search learned me that changing the playbook helps. The old playbook can be found in the previous port but the relevant play for the CSB switches looked like this;

############################
### Cisco Small Business ###
############################
- name: Get config from all Cisco small business devices
  hosts: IOSSB
  gather_facts: no

  tasks:
    - name: Collect Show run from all routers
      raw: "show run"

      register: RUNCFG

    - set_fact: time="{{lookup('pipe','date \"+%Y-%m-%d-%H-%M\"')}}"

    - name: save output to a file
      connection: local
      copy:
        content="\n ===show run=== \n {{ RUNCFG.stdout }}"
        dest="../backups/IOSSB/{{ inventory_hostname }}_run_cfg_{{ time }}.txt"

When I changed the play to the one below this line, the backups worked again.

    ############################
    ### Cisco Small Business ###
    ############################
    - name: Get config from all Cisco small business devices
      hosts: IOSSB
      connection: local
      gather_facts: no
    
      tasks:
      - name: gather running config
        ios_command:
          commands:
            - terminal datadump
            - show run
    #      provider: "{{ cli }}"
    
        register: RUNCFG
    
      - set_fact: time="{{lookup('pipe','date \"+%Y-%m-%d-%H-%M\"')}}"
    
      - name: save output to a file
        connection: local
        copy:
          content="\n ===show run=== \n {{ RUNCFG.stdout }}"
          dest="../backups/IOSSB/{{ inventory_hostname }}_run_cfg_{{ time }}.txt"

It seems the addition of “connection: local” and the “- terminal datapump” IOS command made the difference. A remaining problem with the new playbook is that the backup of small business switches is unreadable because the config file is now all on one line with “\n” where a new line should have been. I make the backup files readable when I need them by running them through this little bash oneliner;

sed 's/\\n/\n/g' unreadable_backup_file > human_readable_file

Of course, should I find a fix for the playbook that makes the original backup file human readable I will update this post. At least with this new play and the oneliner I can backup my small business switches again. I made this post because during my search for a solution I noticed more people had this problem and it was hard to find a clear answer. I cannot find the page where I found the working play anymore, when I do I will credit the original author. I you are the original author of the play then please contact me so I can credit you in this post.

Should you want to buy a Cisco Small Business switch to practise on the Cisco SG350-10 10-Port Gigabit Managed Switch is one of the switches I use at home and one I backup using Ansible. Buying via the link above supports me to write these posts but of course it is perfectly fine to buy somewhere else.

This entry was posted in Cisco, Linux, Networking, Systemen. Bookmark the permalink.