A very simple and easy Ansible playbook to backup (running-config), on any Cisco Router/Switch devices, and save it with hostname and IP-Address as .cfg
- Create an empty YAML file and name it backup.yaml with below codes:
--- - name: Backup Cisco Switches or Router hosts: Cisco connection: local tasks: - name: Collect all facts from the device ios_facts: gather_subset: all #This task optional (to include date in directory name) - name: Get current date exp. 2020-12-31 local_action: command date +%Y-%m-%d register: date #This task optional (to include time in directory name) - name: Get current time exp. 21:46 local_action: command date +%H:%M register: time #Finale task save running-config - name: Save backup running-config ios_config: backup: yes backup_options: filename: "{{ ansible_net_hostname }}_{{ inventory_hostname }}.cfg" dir_path: /YourDirectory/backups/{{ date.stdout }}_{{ time.stdout }}
Tasks date and time are optional and can be removed if you do not want in directory name.
2. Create an empty text file and name it customhosts with device IP-addresses as shown in the image below
3. Run this command:
ansible-playbook -i customhosts backup.yaml -u UserName -k -c local
ansible_net_hostname mean: Device hostname and include in filename
inventory_hostname mean: Device IP address and include in filename