# Cisco IOS backup and write to file with timestamp

**URL:** <https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694>\
**Category:** Ansible Project\
**Created:** [September 30, 2016, 9:39pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694 "2016-09-30T21:39:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Baird](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@James\_Baird](https://forum.ansible.com/u/James_Baird)\
**Post date:** [September 30, 2016, 9:39pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/1 "2016-09-30T21:39:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [September 30, 2016, 11:46pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/2 "2016-09-30T23:46:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_Baird](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@James\_Baird](https://forum.ansible.com/u/James_Baird)\
**Post date:** [October 1, 2016, 12:44am UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/3 "2016-10-01T00:44:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psprygada](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@psprygada](https://forum.ansible.com/u/psprygada)\
**Post date:** [October 2, 2016, 1:27am UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/4 "2016-10-02T01:27:29Z")

</div>

Hi James,

This should accomplish what you are trying to do:

[https://gist.github.com/privateip/d0fd3c6459c5e1a4c1f9dceeb018b109](https://gist.github.com/privateip/d0fd3c6459c5e1a4c1f9dceeb018b109)

Peter

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [October 2, 2016, 6:11am UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/5 "2016-10-02T06:11:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_Baird](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@James\_Baird](https://forum.ansible.com/u/James_Baird)\
**Post date:** [October 3, 2016, 2:38pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/6 "2016-10-03T14:38:30Z")

</div>

That works perfectly. Greatly appreciated

---

<div class="post-metadata">

**Author:** ![AnsiNoob](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@AnsiNoob](https://forum.ansible.com/u/AnsiNoob)\
**Post date:** [October 3, 2016, 9:47pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/7 "2016-10-03T21:47:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Murugan\_Gan](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Murugan\_Gan](https://forum.ansible.com/u/Murugan_Gan)\
**Post date:** [July 9, 2017, 1:56pm UTC](https://forum.ansible.com/t/cisco-ios-backup-and-write-to-file-with-timestamp/22694/8 "2017-07-09T13:56:59Z")

</div>

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
