# backing up a cisco device using telnet

**URL:** <https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823>\
**Category:** Ansible Project\
**Created:** [February 22, 2018, 2:08pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823 "2018-02-22T14:08:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 22, 2018, 2:08pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/1 "2018-02-22T14:08:11Z")

</div>

Is there any information that allows me to backup a cisco device using telnet rather than ssh as the device do not support SSH

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 23, 2018, 2:27pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/2 "2018-02-23T14:27:06Z")

</div>

You need to use the expect module for this.

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 23, 2018, 4:43pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/3 "2018-02-23T16:43:17Z")

</div>

Thanks  
Any examples as there is very scanty information on ansible website for this

Daley

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 23, 2018, 9:01pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/4 "2018-02-23T21:01:18Z")

</div>

There are some in the list if you search the archive, one thread is this one  
[https://groups.google.com/forum/#!msg/ansible-project/iK29xd4QeaE/nNfmvjdOAAAJ](https://groups.google.com/forum/#!msg/ansible-project/iK29xd4QeaE/nNfmvjdOAAAJ)

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 24, 2018, 1:21pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/5 "2018-02-24T13:21:32Z")

</div>

Hi,

You can try using the new (as of Ansible 2.4) “preview” telnet module:

# telnet - Executes a low-down and dirty telnet command

> **[Ansible Documentation — Ansible Community Documentation](https://docs.ansible.com/projects/ansible/latest/index.html)**

Make sure your host file entries resolve in DNS or just use the IP.

Sample host file group:

`

[cisco]  
arctic-sw01 host=10.1.10.100 port=22 username=cisco password=cisco

10.1.10.100 username=cisco password=cisco

`

Sample Playbook - here I am just getting the show inventory output (because its short) and saving it to a variable called output with is a data structure of all the output. Then I take the part I want to save, the actual show inventory output and save it to a file called .txt in the ./FACTs directory.  
You can uncomment the term len 0 and show run commands and do the same.

`

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 26, 2018, 2:32pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/6 "2018-02-26T14:32:25Z")

</div>

Thanks for your reply  
it was very useful  
but I am getting errors

ansible-playbook -i hosts backup\_config-cisco12.yml -vvv  
ansible-playbook 2.4.2.0  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/home/ansible/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/dist-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]  
Using /etc/ansible/ansible.cfg as config file  
Parsed /etc/ansible/hosts inventory source with ini plugin  
PLAYBOOK: backup\_config-cisco12.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in backup\_config-cisco12.yml  
PLAY [cisco] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers  
TASK [run show command via Telnet] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/backup\_config-cisco12.yml:8  
fatal: [10.101.250.12]: FAILED! =\> {  
“changed”: true,  
“msg”: “Telnet action failed: telnet connection closed”,  
“output”:   
}  
…ignoring  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/backup\_config-cisco12.yml:22  
ok: [10.101.250.12] =\> {  
“output”: {  
“changed”: true,  
“failed”: true,  
“msg”: “Telnet action failed: telnet connection closed”,  
“output”:   
}  
}  
TASK [copy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/backup\_config-cisco12.yml:25  
fatal: [10.101.250.12]: FAILED! =\> {  
“msg”: “The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe error appears to have been in ‘/etc/ansible/backup\_config-cisco12.yml’: line 25, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - local\_action: copy content="{{ output.output[0] }}" dest="./etc/ansible/backup{{ inventory\_hostname }}.txt"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n\nexception type: \<class ‘ansible.errors.AnsibleUndefinedVariable’\>\nexception: list object has no element 0”  
}  
…ignoring  
META: ran handlers  
META: ran handlers  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
10.101.250.12 : ok=3 changed=1 unreachable=0 failed=0

This is the config below

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 3:02pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/7 "2018-02-26T15:02:00Z")

</div>

In YAML the rule is if it start with a quote it must end with the same qoute and vice versa.

I recommend using YAML syntax and not Ansible syntax.

&nbsp;&nbsp;- local\_action:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module: copy  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ output.output[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "./etc/ansible/backup{{ inventory\_hostname }}.txt"

Or use delegate\_to: localhost

&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ output.output[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "./etc/ansible/backup{{ inventory\_hostname }}.txt"  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

For completenesses to make the Ansible syntax work

&nbsp;&nbsp;- local\_action: 'copy content="{{ output.output[0] }}" dest="./etc/ansible/backup{{ inventory\_hostname }}.txt"'

I think this also work

&nbsp;&nbsp;- local\_action: copy content={{ output.output[0] }} dest=./etc/ansible/backup{{ inventory\_hostname }}.txt

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 26, 2018, 3:18pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/8 "2018-02-26T15:18:27Z")

</div>

It also looks like your telnet session is failing.

One reason for the error below is an incorrect username or password for the device so check your hosts file.

TASK [configure new account via Telnet] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [10.1.10.100]: FAILED! =\> {“changed”: true, “failed”: true, “msg”: “Telnet action failed: telnet connection closed”, “output”: }  
…ignoring

Finally…make sure the directory you are saving your files to exists so look carefully at your path…did you intend to make that relative to your current working directory (./)?

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 26, 2018, 4:02pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/9 "2018-02-26T16:02:44Z")

</div>

still having errors

ERROR! Syntax Error while loading YAML.

The error appears to have been in ‘/etc/ansible/backup\_config-cisco12.yml’: line 26, column 1, but may  
be elsewhere in the file depending on the exact syntax problem.  
The offending line appears to be:  
module: copy  
content: “{{ output.output[0] }}”  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:  
with\_items:

- {{ foo }}  
Should be written as:  
with\_items:
- “{{ foo }}”  
exception type: \<class ‘yaml.parser.ParserError’\>  
exception: while parsing a block collection  
in “”, line 2, column 1  
did not find expected ‘-’ indicator  
in “”, line 26, column 1

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 4:15pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/10 "2018-02-26T16:15:39Z")

</div>

Since you have syntax error you need to show the code.

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 26, 2018, 5:02pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/11 "2018-02-26T17:02:41Z")

</div>

:x

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 26, 2018, 5:03pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/12 "2018-02-26T17:03:30Z")

</div>

ansible@ansible-new:/etc/ansible$ vi backup\_config-cisco12.yml ignore\_errors: yes  
tasks:

- name: run show command via Telnet  
telnet:  
user: “{{ username }}”  
password: “{{ password }}”  
login\_prompt: "Username: "  
prompts:
- “[\>|#]”  
command:

# - terminal length 0

# - show run

- show inventory  
register: output
- debug: var=output
- local\_action:  
module: copy  
content: “{{ output.output[0] }}”  
dest: “./etc/ansible/backup{{ inventory\_hostname }}.txt”  
27,1

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 5:13pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/13 "2018-02-26T17:13:13Z")

</div>

The "- local\_action" line is indented one space to much.  
In YAML indentation is very impotent and it need to be consistent.

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 26, 2018, 5:18pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/14 "2018-02-26T17:18:44Z")

</div>

As Kai Stian Olstad mentioned, spacing is very important.

Use a good text editor to make sure everything lines up. Below is the playbook using Sublime. Note the subtle lines to help you make sure everything lines up.

 ![Inline image 1](https://us1.discourse-cdn.com/ansible/original/2X/7/7330422c4da0dc08660d2956f8c586243c1bcf94.png)

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 27, 2018, 11:26am UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/15 "2018-02-27T11:26:50Z")

</div>

Thanks  
but still getting errors

this is my code

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 27, 2018, 1:42pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/16 "2018-02-27T13:42:16Z")

</div>

Hi,

I mentioned this before. Are you certain this is the correct path “./etc…”

This means that relative to where you are running your playbook you nave a directory called etc/ansible/backup/

if you are trying to save your backup files to /etc/ansible/backup (absolute path) then you do not need the “.”.

and you must make sure that directory exists.

local\_action: copy content=“{{ output.output[0] }}” dest=“\*\*./etc/ansible/backup/{{ inventory\_hostname }}.\*\*txt”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 27, 2018, 2:11pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/17 "2018-02-27T14:11:02Z")

</div>

> Thanks  
> but still getting errors
> 
> this is my code
> 
> ---  
> &nbsp;&nbsp;hosts: cisco

You are now missing the dash infront of hosts.

I think it's time that you read the documentation and this is a good start  
[https://docs.ansible.com/ansible/latest/YAMLSyntax.html](https://docs.ansible.com/ansible/latest/YAMLSyntax.html)  
[https://docs.ansible.com/ansible/latest/playbooks\_intro.html](https://docs.ansible.com/ansible/latest/playbooks_intro.html)

Study the examples and pay attention to indentation, then look at you code and compare them.

> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;ignore\_errors: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: run show commands via Telnet  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;telnet:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ username }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ password }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;login\_prompt: "Username: "  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompts:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "[\>|#]"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# - terminal length 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# - show run  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- show inventory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: output  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=output.output  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- local\_action: copy content="{{ output.output[0] }}"  
> dest="./etc/ansible/backup/{{ inventory\_hostname }}.txt"  
> and my errors message

And you are back to using code that doesn't work.

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 27, 2018, 2:14pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/18 "2018-02-27T14:14:00Z")

</div>

Also, make sure your playbook is in proper YAML (I suspect your directory does exist as you would get an explicit error for that if it did not)

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 27, 2018, 2:23pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/19 "2018-02-27T14:23:12Z")

</div>

Thanks

Claudia,

I have removed the . from est=“\*\*./etc/ansible/backup/{{ inventory\_hostname }}.\*\*txt” but still having this errors  
it says the hosts I cannot see anything wrong with my hosts or connection

The error appears to have been in ‘/etc/ansible/backup-cisconew.yml’: line 3, column 1, but may  
be elsewhere in the file depending on the exact syntax problem.  
The offending line appears to be:

- hosts: cisco  
connection: local  
^ here  
exception type: \<class ‘yaml.parser.ParserError’\>  
exception: while parsing a block collection  
in “”, line 2, column 1  
did not find expected ‘-’ indicator  
in “”, line 3, column

---

<div class="post-metadata">

**Author:** ![DOkuwa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dokuwa/32/1471_2.png) [@DOkuwa](https://forum.ansible.com/u/DOkuwa)\
**Post date:** [February 27, 2018, 3:36pm UTC](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823/20 "2018-02-27T15:36:01Z")

</div>

[![](https://lh3.googleusercontent.com/-7BXseSRjKMc/WpV60F9P4DI/AAAAAAAAGWw/U3RUeNYjp9oCupen8bo3wyKcNOgkjHB5ACLcBGAs/s320/YAML-new.PNG)](https://lh3.googleusercontent.com/-7BXseSRjKMc/WpV60F9P4DI/AAAAAAAAGWw/U3RUeNYjp9oCupen8bo3wyKcNOgkjHB5ACLcBGAs/s1600/YAML-new.PNG)

Thanks

I am also, seeing this on YAML editing file

[Next page](https://forum.ansible.com/t/backing-up-a-cisco-device-using-telnet/26823.md?page=2)
