Automated router backup code
Ansible Playbook - backup.yml
---
- hosts: routers
gather_facts: no
tasks:
- name: Login to each router and take the backup
vyos.vyos.vyos_config:
backup: yes
backup_options:
filename: "{{ inventory_hostname }}.cfg"
Ansible inventory - inventory:
[routers]
router.a01.labs.nog.mn ansible_host=router.a01.labs.nog.mn lab_user=a01
router.a02.labs.nog.mn ansible_host=router.a02.labs.nog.mn lab_user=a02
router.a03.labs.nog.mn ansible_host=router.a03.labs.nog.mn lab_user=a03
router.a04.labs.nog.mn ansible_host=router.a04.labs.nog.mn lab_user=a04
router.a05.labs.nog.mn ansible_host=router.a05.labs.nog.mn lab_user=a05
router.a06.labs.nog.mn ansible_host=router.a06.labs.nog.mn lab_user=a06
router.a07.labs.nog.mn ansible_host=router.a07.labs.nog.mn lab_user=a07
router.a08.labs.nog.mn ansible_host=router.a08.labs.nog.mn lab_user=a08
router.a09.labs.nog.mn ansible_host=router.a09.labs.nog.mn lab_user=a09
router.a10.labs.nog.mn ansible_host=router.a10.labs.nog.mn lab_user=a10
router.a11.labs.nog.mn ansible_host=router.a11.labs.nog.mn lab_user=a11
router.a12.labs.nog.mn ansible_host=router.a12.labs.nog.mn lab_user=a12
router.a13.labs.nog.mn ansible_host=router.a13.labs.nog.mn lab_user=a13
router.a14.labs.nog.mn ansible_host=router.a14.labs.nog.mn lab_user=a14
router.a16.labs.nog.mn ansible_host=router.a16.labs.nog.mn lab_user=a16
router.a17.labs.nog.mn ansible_host=router.a17.labs.nog.mn lab_user=a17
router.a18.labs.nog.mn ansible_host=router.a18.labs.nog.mn lab_user=a18
router.a19.labs.nog.mn ansible_host=router.a19.labs.nog.mn lab_user=a19
router.a20.labs.nog.mn ansible_host=router.a20.labs.nog.mn lab_user=a20
router.a21.labs.nog.mn ansible_host=router.a21.labs.nog.mn lab_user=a21
router.a22.labs.nog.mn ansible_host=router.a22.labs.nog.mn lab_user=a22
router.a23.labs.nog.mn ansible_host=router.a23.labs.nog.mn lab_user=a23
router.a24.labs.nog.mn ansible_host=router.a24.labs.nog.mn lab_user=a24
router.a25.labs.nog.mn ansible_host=router.a25.labs.nog.mn lab_user=a25
router.a26.labs.nog.mn ansible_host=router.a26.labs.nog.mn lab_user=a26
router.a27.labs.nog.mn ansible_host=router.a27.labs.nog.mn lab_user=a27
router.a28.labs.nog.mn ansible_host=router.a28.labs.nog.mn lab_user=a28
router.a29.labs.nog.mn ansible_host=router.a29.labs.nog.mn lab_user=a29
router.a30.labs.nog.mn ansible_host=router.a30.labs.nog.mn lab_user=a30
router.a31.labs.nog.mn ansible_host=router.a31.labs.nog.mn lab_user=a31
router.a32.labs.nog.mn ansible_host=router.a32.labs.nog.mn lab_user=a32
router.a33.labs.nog.mn ansible_host=router.a33.labs.nog.mn lab_user=a33
router.a34.labs.nog.mn ansible_host=router.a34.labs.nog.mn lab_user=a34
router.a35.labs.nog.mn ansible_host=router.a35.labs.nog.mn lab_user=a35
router.a36.labs.nog.mn ansible_host=router.a36.labs.nog.mn lab_user=a36
router.a37.labs.nog.mn ansible_host=router.a37.labs.nog.mn lab_user=a37
[routers:vars]
ansible_user=anurag
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=vyos.vyos.vyos
Gitlab CI config file - .gitlab-ci.yml
stages:
- Backup
vyos_take_backup:
image: registry.gitlab.com/anuragbhatia/btnog10-router-backup/ansible:latest
stage: Backup
script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- git config --global user.email "me@anuragbhatia.com"
- git config --global user.name "Anurag Bhatia"
- cd /root
- git clone git@gitlab.com:anuragbhatia/btnog10-router-backup.git
- cd btnog10-router-backup
- ansible-playbook -i inventory backup.yml
- git add backup && git commit -m "Changes in router config" && git push
- exit
only:
- web
- schedules