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
- 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.