backing up a cisco device using telnet

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

You need to use the expect module for this.

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

Daley

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

Hi,

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

telnet - Executes a low-down and dirty telnet command

http://docs.ansible.com/ansible/2.4/telnet_module.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.

`

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

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.

  - local_action:
      module: copy
      content: "{{ output.output[0] }}"
      dest: "./etc/ansible/backup{{ inventory_hostname }}.txt"

Or use delegate_to: localhost

  - copy:
      content: "{{ output.output[0] }}"
      dest: "./etc/ansible/backup{{ inventory_hostname }}.txt"
    delegate_to: localhost

For completenesses to make the Ansible syntax work

  - local_action: 'copy content="{{ output.output[0] }}" dest="./etc/ansible/backup{{ inventory_hostname }}.txt"'

I think this also work

  - local_action: copy content={{ output.output[0] }} dest=./etc/ansible/backup{{ inventory_hostname }}.txt

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 (./)?

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

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

:x

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

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

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.

Thanks
but still getting errors

this is my code

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”

Thanks
but still getting errors

this is my code

---
  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/playbooks_intro.html

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

  connection: local
  ignore_errors: yes
  tasks:
      - name: run show commands via Telnet
        telnet:
          user: "{{ username }}"
          password: "{{ password }}"
          login_prompt: "Username: "
          prompts:
            - "[>|#]"
          command:
            # - terminal length 0
            # - show run
            - show inventory
        register: output
      - debug: var=output.output
      - 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.

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)

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

Thanks

I am also, seeing this on YAML editing file