I have created a very simple and easy Ansible playbook version 2 to backup running-config Cisco Router or Switch devices and save it with hostname and IP-Address as .cfg
The playbook automatically create a new directory every time you run it with date and time as name.
As you can see in 00:45 sec. on the attached video at the end of the page.
- Create an empty YAML file and name it backup.yaml with below codes:
# @author [Hawar Koyi] # @email [hawar.koy@gmail.com] # @create date 2021-06-29 17:12:00 # @desc [Automation - Backup Cisco Router or Switch] --- - name: Backup Cisco Router or Switch hosts: Cisco connection: network_cli gather_facts: false tasks: - name: Accespt ansible net hostname ios_facts: gather_subset: all - name: Get current date local_action: command date +%Y-%b-%d register: date - name: Get current time local_action: command date +%H:%M register: time - name: Get running-config and save it ios_config: backup: yes backup_options: #ansible_host = getting device IP, ansible_net_hostname = getting device hostname filename: "{{ ansible_net_hostname }}_{{ ansible_host }}.cfg" #Please change /home/hawar/Desktop to your direcotry #Every time create a new directory under named date and time dir_path: /home/hawar/Desktop/{{ date.stdout }}_at_{{ time.stdout }} - name: Tasks done debug: msg="All tasks has been done on {{ date.stdout }} at {{ time.stdout }}." #End of codes
2. Create an empty text file and name it hosts with devices IP-addresses and the defaults values.
As you can see I only added one device, otherwise you can add more than one device, if they have the same username and password.
[Cisco] R1 ansible_host=192.168.10.38 [Cisco:vars] ansible_connection=network_cli ansible_network_os=ios ansible_user=cisco ansible_password=cisco ansible_become=yes ansible_become_method=enable ansible_become_password=cisco
ansible_password: Device SSH password
ansible_become_password: Device enable password
3. Run this command:
ansible-playbook -i hosts backup.yaml
Additional, watch my created video about this Ansible playbook.