Cisco IOS backup and write to file with timestamp

3,215 views
Skip to first unread message

James Baird

unread,
Sep 30, 2016, 6:25:22 PM9/30/16
to Ansible Project
I am relatively knew to ansible and need a little assistance. We are trying to use ansible to backup our Cisco configuration. I figured that this would be fairly simple and I have made some progress but I am stuck at the moment.

I want to be able to run this playbook and have it output the configuration of the cisco router to a simple text file on the ansible server. The file name needs to be the current time when it ran in the format MMDDYYYY. I want to deposit the file in a folder that is the hostname of the switch defined in the inventory file.

I can get the playbook to run when I use the hostname for the file name and do not include the directory based on hostname, but the timestamp from the ansible server is failing due to the length of the filename being to long. Also the text of the configuration file is devoid of formatting so it is in a single line with quotation around each entry from the config. Here is a snippet from the file output:

{"changed": false, "cisco.stdout_lines": [["Building configuration...", "", "Current configuration : 3389 bytes", "!", "version 12.2", "no service pad",

So the items I need help with are:
  1. Format the show run so that we can use it for a copy paste restore at some point
  2. Attach the local timestamp to the filename
  3. Attach the hostname to the folder that the backup is placed
Running ansible 2.1.2

Here is my playbook so far (or what I thought would work)...
=================================================================
---
- hosts: switches
  connection: local
  vars:
    cli:
      host: "{{ inventory_hostname }}"
      username: circle
      password: Circle86
      transport: cli
      backup: yes
  tasks:
    - name: timestamp
      local_action: command date +%Y%m%d
      register: timestamp

    - name: run show running-config on remote devices
      ios_command:
        commands: show running
        provider: "{{ cli }}"
      register: cisco
     
    - name: debug
      debug:
        var: cisco.stdout_lines
      register: debug
     
    - copy: content={{ debug }} dest=/home/james/ansible/Cisco/test3/backups/{{ inventory_hostname }}/{{ timestamp }}
=============================================================================================

While I know there are other methods of doing this through SCP or FTP and the archive command on cisco, we are trying to keep this centralized within Ansible since we manage many linux servers from there as well.

Thoughts?

Thanks

AnsiNoob

unread,
Sep 30, 2016, 8:10:52 PM9/30/16
to Ansible Project
use iOS_config module instead. It has backup option in it and it creates backup dir in the folder from where you are running your playbook. Also, it adds hostname,date, time to backup file so you are all set. Below should work .. 

- name: take backup
  ios_config:
    backup: yes
    authorize: yes
    provider: "{{ cli }}"

James Baird

unread,
Sep 30, 2016, 8:44:18 PM9/30/16
to Ansible Project
I looked into going that route but the concern is that is only available in 2.2 and I may have a hard time pushing the team to upgrade Ansoble to facilitate that change when we will be using this in production.

Peter Sprygada

unread,
Oct 1, 2016, 9:27:42 PM10/1/16
to ansible...@googlegroups.com
Hi James,

This should accomplish what you are trying to do:


Peter



On Fri, Sep 30, 2016 at 8:44 PM, James Baird <jame...@gmail.com> wrote:
I looked into going that route but the concern is that is only available in 2.2 and I may have a hard time pushing the team to upgrade Ansoble to facilitate that change when we will be using this in production.

--
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 ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7561f2e8-004b-48e0-8f73-9e385f55e1b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Salman Siddiqui

unread,
Oct 2, 2016, 2:11:41 AM10/2/16
to ansible...@googlegroups.com
^^ Yes, this looks great. James, do tell us if this helps with your formatting as well. If not then you may have to run regex or sed replacing \n with \n. That is when all \n will actually be read as next line.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/-WhcKGO0NXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.

Jimbo

unread,
Oct 3, 2016, 10:38:31 AM10/3/16
to Ansible Project
That works perfectly. Greatly appreciated


On Saturday, October 1, 2016 at 8:27:42 PM UTC-5, Peter Sprygada wrote:
Hi James,

This should accomplish what you are trying to do:


Peter


On Fri, Sep 30, 2016 at 8:44 PM, James Baird <jame...@gmail.com> wrote:
I looked into going that route but the concern is that is only available in 2.2 and I may have a hard time pushing the team to upgrade Ansoble to facilitate that change when we will be using this in production.

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Salman Siddiqui

unread,
Oct 3, 2016, 5:48:19 PM10/3/16
to Ansible Project
Does this solve your problem where all text was in same line and not different lines?
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/-WhcKGO0NXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Murugan Gan

unread,
Jul 10, 2017, 11:46:52 AM7/10/17
to Ansible Project
Hi Team,

I have created sh running config getting output but have 150 routers but it triggered one notepad text it include all config output any chance to get trigger each host name with each text output.

Regards,

S.Manivannan
Reply all
Reply to author
Forward
0 new messages