APNIC 56 Network Automation tutorial
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

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]
r01.apnic56.anuragbhatia.com ansible_host=r01.apnic56.anuragbhatia.com
r02.apnic56.anuragbhatia.com ansible_host=r02.apnic56.anuragbhatia.com
r03.apnic56.anuragbhatia.com ansible_host=r03.apnic56.anuragbhatia.com

[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