This Article will contain Ansible playbook setup example against Cisco Routers.
Ansible notes will be posted here and continuesly updated on the fly when required.
Ansible environment as always devided for 2 servers: PRODuction and DEVelopment.
Tested scrypts will be moved from DEV into PROD server once fully tested and ready.
Host file content:
Ansible configuration files location: etc/ansible
[CiscoASR]
ASR01 ansible_host=10.x.x.x1
ASR02 ansible_host=10.x.x.x2ASR03 ansible_host=10.x.x.x3
ASR04 ansible_host=10.x.x.x4
[CiscoASR:vars]
ansible_user=admin
ansible_ssh_pass=****
Manual execution (for test):
ansible-playbook ciscobackup.yml
Output files location:
ls home/administrator/network-programmability/
Scheduling done over crontub:
crontab -e
0 1 * * * if ! out=`ansible-playbook etc/ansible/cisco.yml`; then echo $out; fi
Playbook:
---
## Playbook to get system time and append it to backup files
## Made by DM
## Last change 09/06/2021
- hosts: localhost
tasks:
- name: Get ansible date/time facts
setup:
filter: "ansible_date_time"
gather_subset: "!all"
- name: Store DTG as fact
set_fact:
DTG: "{{ ansible_date_time.date }}"
- name: Create Directory {{hostvars.localhost.DTG}}
file:
path: ~/network-programmability/backups/ASR/{{hostvars.localhost.DTG}}
state: directory
run_once: true
- hosts: CiscoASR
gather_facts: false
connection: local
tasks:
- name: Show IP int brief
ios_command:
commands: show ip int brief
register: config
- name: Save output to /home/administrator/network-programmability/backups/ASR/
copy:
content: "{{config.stdout[0]}}"
dest: "/home/administrator/network-programmability/backups/ASR/{{hostvars.localhost.DTG}}/{{inventory_hostname}}-{{hostvars.localhost.DTG}}-showipint.txt"
- hosts: CiscoASR
gather_facts: false
connection: network_cli
tasks:
- name: Backup Config
ios_config:
backup: yes
backup_options:
filename: "{{inventory_hostname}}-{{hostvars.localhost.DTG}}-config.txt"
dir_path: /home/administrator/network-programmability/backups/ASR/{{hostvars.localhost.DTG}}
become: yes
become_method: enable
- name: Show History All
ios_command:
commands: show history all
register: shhistall
become: yes
become_method: enable
- name: Save output to /home/administrator/network-programmability/backups/ASR/
copy:
content: "{{shhistall.stdout[0]}}"
dest: "/home/administrator/network-programmability/backups/ASR/{{hostvars.localhost.DTG}}/{{inventory_hostname}}-{{hostvars.localhost.DTG}}-showhistall.txt"
- hosts: CiscoASR
gather_facts: false
connection: local
vars:
command_list:
- show clock
- sh ip ospf neighbor
tasks:
- name: Run the SHOW commands and save output
ios_command:
commands: "{{ command_list }}"
register: showoutput
- name: "Put all the files together into one nice text file"
template:
src: template.j2
dest: "/home/administrator/network-programmability/backups/ASR/{{hostvars.localhost.DTG}}/{{ inventory_hostname }}-{{hostvars.localhost.DTG}}-showoutput.txt"
template.j2
OUTPUT FROM SHOW COMMANDS for: {{ inventory_hostname }} {% for cmd in command_list %}
############ {{ cmd }} ################ {{ showoutput.stdout[loop.index0] }} {% endfor %}