Cisco IOS backup and write to file with timestamp

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)…

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 }}”

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.

Hi James,

This should accomplish what you are trying to do:

https://gist.github.com/privateip/d0fd3c6459c5e1a4c1f9dceeb018b109

Peter

^^ 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.

That works perfectly. Greatly appreciated

Does this solve your problem where all text was in same line and not different lines?

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