Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu
Ansible backup configuration

How to backup network devices using Ansible – Part I

Posted on September 15, 2022September 15, 2022 by Gurpreet Kochar

After a lot of procrastination and apprehension to learn Ansible for reasons that network industry folks are well aware of, I finally have to learn Ansible because the job demands it now. Of course, one of the primary tasks is to learn how to back up the configuration of network devices using Ansible. In this post, we are going to see the basics of how to backup network devices using Ansible

Step1:- Define your .cfg and hosts file

Step2:- Start writing the playbook ( assuming you have the basic understanding of writing playbooks)

- name: Backup configuraiton
  connection: network_cli
  hosts: all
  gather_facts: true
  tasks:
    - block:
        - name: Get current date
          local_action: command date +%d-%b-%Y
          register: date

        - name: Get current time
          local_action: command date +%H:%M
          register: time
      run_once: true

    - name: Get running-config for all IOS and save it
      ios_config:
        backup: yes
        backup_options:
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: "./backup/{{ date.stdout }}_at_{{ time.stdout }}/{{ansible_network_os}}"
      when: "ansible_network_os == 'ios'"

    - name: Get running-config for all ARISTA and save it
      eos_config:
        backup: yes
        backup_options:
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: "./backup/{{ date.stdout }}_at_{{ time.stdout }}/{{ansible_network_os}}"
      when: "ansible_network_os == 'eos'"

    - name: Get running-config for all JUNOS and save it
      junos_config:
        backup: yes
        backup_options:
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: "./backup/{{ date.stdout }}_at_{{ time.stdout }}/{{ansible_network_os}}"
      when: "ansible_network_os == 'junos'"

    - name: Get running-config for all NXOS and save it
      nxos_config:
        backup: yes
        backup_options:
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: "./backup/{{ date.stdout }}_at_{{ time.stdout }}/{{ansible_network_os}}"
      when: "ansible_network_os == 'nxos'"

Explanation of the above playbook:-

     - block:
        - name: Get current date
          local_action: command date +%d-%b-%Y
          register: date

        - name: Get current time
          local_action: command date +%H:%M
          register: time
      run_once: true
  1. I have tagged this code block to run_once because we don’t want to find the date and time for each host of the inventory file. We only need to calculate it once and then use that to create the folders for all the hosts.
    - name: Get running-config for all IOS and save it
      ios_config:  >>> the relevant module from ansible documentation
        backup: yes >>> yes to save the backup
        backup_options:
#        "ansible_net_hostname" is the hostname of the device as discovered by gather_facts module.
#        "ansible_host" is the IP of the device as discovered by gather_facts module.
#        "date.stdout and time.stdout" is the date and time as discovered by the above code block
#        "ansible_network_os" is the operation system of the device that you have defined in inventory variables
          filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
          dir_path: "./backup/{{ date.stdout }}_at_{{ time.stdout }}/{{ansible_network_os}}"
      when: "ansible_network_os == 'ios'"

# This task will only be execute for devices which have their ansible_network_os == ios and skipped for others like juniper, nexus etc

Execute the above playbook with the result in an output like this below

On each execution, it will create a folder with the current date and time and create folders inside that folder based on the operating system of the device and place the backup configuration under each relevant folder.

There is an alternate way to get the date and time for the above task.

     - block:
        - name: Gather datetime
          ansible.builtin.setup:
            gather_subset: date_time
          delegate_to: 127.0.0.1
          delegate_facts: true
          register: facts

        - name: Print
          ansible.builtin.debug:
            msg: "{{facts['ansible_facts']['ansible_date_time']}}"
       run_once: true

The advantage of this is you can gather a subset of facts as you need. You can read more about the ansible setup module from its documentation. To see why it’s better, let’s see what delegate_to means. Essentially you are telling the playbook/ansible to give away that task to 127.0.0.1 to gather facts from which we can further extract the DateTime and run it only once which is probably faster in my opinion.

In part II of how to backup network devices using ansible, we will see how to export the backup configuration to a remote server like a TFTP or directory to a GitHub repository(source version control)

There are definitely better ways of writing a playbook. I am still learning. If you have any suggestions, please feel free to post them in the comment box.

Know someone who may benefit? Share this:

  • Tweet
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Email a link to a friend (Opens in new window) Email
  • More
  • Print (Opens in new window) Print
  • Share on Reddit (Opens in new window) Reddit
  • Share on Tumblr

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2026 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d