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 configuration to TFTP Server using Ansible – Part II

Posted on September 19, 2022September 17, 2022 by Gurpreet Kochar

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

CISCO OUTPUT
ARISTA OUTPUT

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.

CLI_COMMAND MODULE

IOS_COMMAND MODULE

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.

Know someone who may benefit? Share this:

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

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
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d