Skip to content

Latest commit

 

History

History
114 lines (106 loc) · 2.55 KB

11 Cisco Switch Target.md

File metadata and controls

114 lines (106 loc) · 2.55 KB

Cisco Switch Targets

Since Ansible can configure lot of target types, we look at Cisco devices now. The only tricky thing is to get a working example config, so lets create one.

Create Ansible Project Files

Project

PNAME="Cisco_Switch"
PDIR="/etc/ansible/projects/cisco_switch"
mkdir -p $PDIR
chmod 700 $PDIR
cd $PDIR

Inventory

  • $PDIR/inventory
# ansible demo inventory for $PNAME
[cisco_switch]
switch1

Ansible Config

  • $PDIR/ansible.cfg
# custom ansible $PNAME configuration
[defaults]
inventory      = ./inventory
roles_path    = ./roles
collections_paths = ./collections
remote_user = root
log_path = ./ansible.log
host_key_checking = False

Group Vars

  • $PDIR/group_vars/cisco_switch.yml
---
# credentials for cisco switches
creds:
  host: "{{ inventory_hostname }}"
  username: admin
  password: sshPassword...
  auth_pass: enablePassword...
  authorize: yes
...

Playbook

  • $PDIR/cisco_switch_backup.yml
---
- hosts: cisco_switch
  gather_facts: no
  connection: local
  vars:
    backup_dir: ./backup
  tasks:
  - name: save running config to device
    ios_config:
      save_when: always
      provider: "{{ creds }}"
  - name: get cisco switch config
    ios_command:
      commands: 
      - show running-config
      provider: "{{ creds }}"
    register: config
  - name: ensure backup folder is created
    file:
      path: "{{ backup_dir }}/"
      state: directory
    run_once: yes
  - name: get timestamp
    command: date +%Y%m%d
    register: timestamp
    run_once: yes
  - name: save device config to {{ backup_root }} 
    copy: 
      content: "{{ config.stdout[0] }}"
      dest: "{{ backup_dir }}/show_run_{{ inventory_hostname }}_{{ timestamp.stdout }}.txt"
  - name: get device information
    ios_command:
      commands:
      - show vlan brief
      - show interface status
      - show ip arp
      - show cdp neighbors
      - show version
      provider: "{{ creds }}"
    register: config
  - name: save device information to {{ backup_root }}
    copy:
      content: |
        {{ config.stdout[0] }}
        ----------------------
        {{ config.stdout[1] }}
        ----------------------
        {{ config.stdout[2] }}
        ----------------------
        {{ config.stdout[3] }}
        ----------------------
        {{ config.stdout[4] }}
      dest: "{{ backup_dir }}/doku_{{ inventory_hostname }}_{{ timestamp.stdout }}.txt"
...