In Part I of the Ansible series, we looked at how to backup the configuration of network devices like cisco, juniper, arista, etc to the local disk of the ansible control node. However, in this post, we will see how we can take the backup of the configuration directly to the TFTP server using Ansible.
There are 2 possible ways of achieving this.
1. Take backup directly from within the devices over to TFTP
Like in the case of cisco, you can execute, and copy running-config tftp://<ip_address> to backup the configuration directly from the cisco device over to TFTP. We can tell Ansible to do the exact same using the command / cli_command module for each vendor.
For the sake of brevity, I am not going to copy-paste the entire playbook from PartI but I am only going to paste the part that actually needs modification to achieve the end goal.
- block:
- 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}}"
- name: "BACKUP: IOS CONFIG to TFTP"
ansible.netcommon.cli_command:
command: copy running-config tftp://172.16.14.200
prompt: "Address or name of remote host \\[172.16.14.200\\]?"
answer: "\n"
when: ansible_network_os == 'ios'
- block:
- 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}}"
- name: "BACKUP: ARISTA CONFIG to TFTP"
ansible.netcommon.cli_command:
command: "copy running-config tftp://172.16.14.200/{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
when: "ansible_network_os == 'eos'"
The important bit here to understand is how the CLI behaves to the command when you do it manually. There are some IOS versions/platforms where the command produces interactive results like in the case of IOS however in the case of Arista devices, such is not the case, there is no interactivity. Here are the screenshots depicting the difference in the behavior of the 2 CLIs
The above playbook is only trying to mimic how the CLI behaves. Instead of using the “cli_command” module, we could have also used the ios_command modules for the same end goal. There is a subtle difference, take a look at the below two references for the alternate.
Another method for this could be to dump the configs on the local drive and then use the Ansible shell module to upload files from localhost to the remote storage server using any of the transfer protocols.
EDIT:-
It turns out, that if you configure the cisco IOS with this command, then you don’t need to handle the interactivity of the CLI while transferring configuration from the device to the TFTP server making your playbook more uniform.
DC_RTR(config)#file prompt quiet
DC_RTR#copy running-config tftp://172.16.14.200
!!
3344 bytes copied in 2.978 secs (1123 bytes/sec)
Hence your IOS task will look exactly like the EOS task for taking the backups.
- block:
- 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}}"
- name: "BACKUP: IOS CONFIG to TFTP"
ansible.netcommon.cli_command:
command: "copy running-config tftp://172.16.14.200/{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
when: ansible_network_os == 'ios'
- block:
- 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}}"
- name: "BACKUP: ARISTA CONFIG to TFTP"
ansible.netcommon.cli_command:
command: "copy running-config tftp://172.16.14.200/{{ ansible_net_hostname }}_{{ ansible_host }}.cfg"
when: "ansible_network_os == 'eos'"
In PartIII of this series, we will see how to take backup to Git repositories for version controlling instead of tftp or local backups.