Ansible: Backing up Existing Configurations




While some networks are built from the ground up with Ansible using modules or templates, it is possible to grab the networking configuration from a pre-configured network, and even push it back out to the switch.

This type of model can be beneficial for:

  • Upgrade scenarios
  • Taking a snapshot of the network before trying something new
  • Reverting quickly back to a snapshot
  • Providing an introduction to automation


Ansible Modules

The two examples below primarily use these four core Ansible modules:

Example Fetch

On the server is a folder with one file called fetch.yml.

user@server ~/consulting/fetch $ ls

The content of the file is very simple:

- hosts: leaf1
  become: yes
    - name: fetch ports.conf
      fetch: dest=save/{{ansible_hostname}}/ports.conf src=/etc/cumulus/ports.conf flat=yes

    - name: fetch interfaces
      fetch: dest=save/{{ansible_hostname}}/interfaces src=/etc/network/interfaces flat=yes

    - name: copy quagga daemons file
      fetch: dest=save/{{ansible_hostname}}/daemons src=/etc/quagga/daemons flat=yes

    - name: copy Quagga.conf
      fetch: dest=save/{{ansible_hostname}}/Quagga.conf src=/etc/quagga/Quagga.conf flat=yes

To run the playbook, run the ansible-playbook command:

user@server ~/consulting/fetch $ ansible-playbook fetch.yml

PLAY [leaf1] ******************************************************************

GATHERING FACTS ***************************************************************
ok: [leaf1]

TASK: [fetch ports.conf] ******************************************************
changed: [leaf1]

TASK: [fetch interfaces] ******************************************************
changed: [leaf1]

TASK: [copy quagga daemons file] **********************************************
changed: [leaf1]

TASK: [copy Quagga.conf] ******************************************************
ok: [leaf1]

PLAY RECAP ********************************************************************
leaf1                      : ok=5    changed=3    unreachable=0    failed=0

The playbook copies these four commonly used Cumulus Linux files to the server:

File Name Description
/etc/cumulus/ports.conf Configuration for breakout ports
/etc/network/interfaces Network configuration File
/etc/quagga/daemons Daemons configuration file for Quagga
/etc/quagga/Quagga.conf Quagga Configuration file

For more information on which files to back up and what Cumulus Linux uses, please refer to Upgrading Cumulus Linux.

The playbook copies the files to a directory called save:

user@server ~/consulting/fetch $ ls
fetch.yml  save

The playbook puts the files into a directory based on the hostname. This particular example shows the playbook was run only on one switch named leaf1:

user@server ~/consulting/fetch/save $ ls

All the files are stored in the leaf1 directory:

user@server ~/consulting/fetch/save/leaf1 $ ls
daemons  interfaces  ports.conf  Quagga.conf

Example Copy

On the server a file called copy.yml was added to the directory; the file has this content:

- hosts: leaf1
  become: yes
    - name: copy ports.conf
      copy: src=save/{{ansible_hostname}}/ports.conf dest=/etc/cumulus/
    - name: copy interfaces
      copy: src=save/{{ansible_hostname}}/interfaces dest=/etc/network/
    - name: copy Quagga daemons
      copy: src=save/{{ansible_hostname}}/daemons dest=/etc/quagga/
    - name: copy Quagga conf
      copy: src=save/{{ansible_hostname}}/Quagga.conf dest=/etc/quagga/

    - name: reload switchd
      service: name=switchd state=restarted
    - name: reload networking
      command: ifreload -a
    - name: reload quagga
      service: name=quagga state=restarted

This file simply pushes back the files that were already saved, then restarts the corresponding services using the service and command module. Instead of issuing a service=networking command, the ifreload -a command was run directly.

user@server ~/consulting/fetch $ ansible-playbook copy.yml

PLAY [leaf1] ******************************************************************

GATHERING FACTS ***************************************************************
ok: [leaf1]

TASK: [copy ports.conf] *******************************************************
ok: [leaf1]

TASK: [copy interfaces] *******************************************************
ok: [leaf1]

TASK: [copy Quagga daemons] ***************************************************
ok: [leaf1]

TASK: [copy Quagga conf] ******************************************************
changed: [leaf1]

TASK: [reload switchd] ********************************************************
changed: [leaf1]

TASK: [reload networking] *****************************************************
changed: [leaf1]

TASK: [reload quagga] *********************************************************
changed: [leaf1]

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/user/copy.retry

leaf1                      : ok=8    changed=4    unreachable=0    failed=0

Now the files have been pushed back to the switch, which is operating on the previous snapshot.

The save directory could be based on the time of day rather than a generic folder called save by using:


You can find more information on which facts Ansible gathers by reading this KB article.

Have more questions? Submit a request


Powered by Zendesk