Skip to content

Instantly share code, notes, and snippets.

@privateip
Created October 2, 2016 01:26
Show Gist options
  • Save privateip/d0fd3c6459c5e1a4c1f9dceeb018b109 to your computer and use it in GitHub Desktop.
Save privateip/d0fd3c6459c5e1a4c1f9dceeb018b109 to your computer and use it in GitHub Desktop.
playbook to backup network device running configuration to local file
---
- hosts: ios
connection: local
vars:
backup_root: /tmp/backups
cli:
host: "{{ inventory_hostname }}"
username: cisco
password: cisco
transport: cli
tasks:
- name: run show running-config on remote devices
ios_command:
commands: show running
provider: "{{ cli }}"
register: config
- name: ensure backup folder is created
file:
path: "{{ backup_root }}"
state: directory
run_once: yes
- name: ensure device rolder is created
file:
path: "{{ backup_root }}/{{ inventory_hostname }}"
state: directory
- name: get timestamp
command: date +%Y%m%d
register: timestamp
- copy:
content: "{{ config.stdout[0] }}"
dest: "{{ backup_root }}/{{ inventory_hostname }}/running-config_{{ timestamp.stdout }}"
@opnine
Copy link

opnine commented Jul 9, 2017

Hello ,

THanks for you code but I get this error.

fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "unable to open shell. Please see: https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", "rc": 255}
!
ansible 2.3.1.0

How can i fix this?

thanks.

@jacksonp2008
Copy link

Say, this works fine except the config file is in some kind of strange format, doubt this will paste back into a switch. Have you tried it?

@jacksonp2008
Copy link

take that back, ,stdout[0] fixed the format. I had missed that.

@TheEric7
Copy link

TheEric7 commented Oct 5, 2017

Hi,

Getting this error:

fatal: [R4]: FAILED! => {"changed": false, "failed": true, "msg": "show running-config\r\n ^\r\n% Invalid input detected at '^' marker.\r\n\r\nR1>", "rc": 1}

Not sure what the problem is, but it's somehow related to the "show running" command that we're using. Cos when I changed it to "show version" it works perfectly as expected.

How can this be fixed?

Cheers

@toms3t
Copy link

toms3t commented Feb 18, 2018

I'm having the same problem as EricH7777777. Any solutions here?

@Melanos
Copy link

Melanos commented Mar 26, 2018

Hey Guys!

Regarding EricH7777777 and toms3t issue. I had same issue, in order to run: show running config - switch must be in privileged mode for cisco devices. <>.

So, in a playbook you have to add --> authorize: yes and auth_pass: cisco (auth_pass is enable password on the switch side).

my creds.yml file:

creds:
username: cisco
password: cisco
auth_pass: cisco

This is my playbook:

  • hosts: myswitches
    connection: local

    vars:
    backup_root: /etc/ansible/backups

    tasks:

    • name: GET CREDENTIALS
      include_vars: creds.yml

    • name: DEFINE CONNECTION
      set_fact:
      connection:
      authorize: yes // make sure that "authorize: yes" is there, otherwise it wont go to enable mode.
      host: "{{ inventory_hostname }}"
      username: "{{ creds['username'] }}"
      password: "{{ creds['password'] }}"
      auth_pass: "{{ creds['auth_pass'] }}"

    • name: run show running-config on remote devices
      ios_command:
      commands: show running
      provider: "{{ connection }}"
      register: config

    • name: ensure backup folder is created
      file:
      path: "{{ backup_root }}"
      state: directory
      run_once: yes

    • name: ensure device rolder is created
      file:
      path: "{{ backup_root }}/{{ inventory_hostname }}"
      state: directory

    • name: get timestamp
      command: date +%Y%m%d
      register: timestamp

    • copy:
      content: "{{ config.stdout[0] }}"
      dest: "{{ backup_root }}/{{ inventory_hostname }}/running-config_{{ timestamp.stdout }}"

Maybe there is a better way to shorten playbook, if anyone have any suggestions it would be great.

hope this helps, let me know if you need any more help.

@harrypuru
Copy link

provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match

@harrypuru
Copy link

Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in "/tmp". Failed command was: ( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-2756uxxwdzsa/ansible-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment