Create an Ansible Role that can back up the configuration of managed network nodes made by different vendors.
Outcomes
Create a role that backs up configuration files for Junos and IOS managed network nodes, and then stores those backup files in a separate location.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command also creates a project directory with the files needed for the exercise.
[student@workstation ~]$ lab start simplify-roles
Instructions
Open VS Code and switch to the tab, or open a GNOME terminal, and create the directory structure for a role named net_backup.
The role must include the meta, tasks, and vars directories.
Run the following commands to create the role and display the initial role structure and files:
[student@workstation ~]$cd /home/student/simplify-roles[student@workstation simplify-roles]$mkdir roles; cd roles[student@workstation roles]$ansible-galaxy init net_backup- Role net_backup was created successfully [student@workstation roles]$rm -rvf \net_backup/{files,defaults,templates,handlers,tests}...output omitted... [student@workstation roles]$tree net_backup/net_backup/ ├── README.md ├── meta │ └── main.yml ├── tasks │ └── main.yml └── vars └── main.yml 3 directories, 4 files
Open the /home/student/simplify-roles directory in VS Code.
Create the roles/net_backup/tasks/junos_netconf_enable.yml task file to enable NETCONF on the Junos managed nodes.
Create the roles/net_backup/tasks/backup_junos.yml task file to back up the configurations on the Junos managed nodes.
Create the roles/net_backup/tasks/backup_ios.yml task file to back up the configurations on the IOS managed nodes.
Open VS Code and then click → .
Navigate to → and click .
If prompted, select , and then click .
Create a task file named roles/net_backup/tasks/junos_netconf_enable.yml to enable NETCONF on the Junos managed nodes.
The completed file must consist of the following content:
---
- name: Enable the netconf service on port 830
vars:
ansible_connection: ansible.netcommon.network_cli
junipernetworks.junos.junos_netconf:
netconf_port: 830
state: presentCreate a task file named roles/net_backup/tasks/backup_junos.yml to back up the configurations on the Junos managed nodes.
The completed file must consist of the following content:
---
- name: Backup Junos configurations
junipernetworks.junos.junos_config:
backup: true
backup_options:
dir_path: "{{ backup_path }}"
filename: "{{ inventory_hostname }}.cfg"Create a task file roles/net_backup/tasks/backup_ios.yml to back up the configurations on the IOS managed nodes.
The completed file must consist of the following content:
---
- name: Backup IOS XE configurations
cisco.ios.ios_config:
backup: true
backup_options:
dir_path: "{{ backup_path }}"
filename: "{{ inventory_hostname }}.cfg"Create a task file named roles/net_backup/tasks/junos.yml.
Add two tasks to the file to include the junos_netconf_enable.yml and backup_junos.yml task files.
The completed file must consist of the following content:
---
- name: Enable NETCONF
ansible.builtin.include_tasks:
file: junos_netconf_enable.yml
- name: Create Junos backup files
ansible.builtin.include_tasks:
file: backup_junos.ymlCreate the roles/net_backup/tasks/ios.yml file.
Add a task to the file that includes the tasks/backup_ios.yml task file.
The completed file must consist of the following content:
---
- name: Create IOS backup files
ansible.builtin.include_tasks:
file: tasks/backup_ios.ymlDefine the role variable called backup_path in the roles/net_backup/vars/main.yml file.
Set the backup_path variable to the backups value.
The completed file must consist of the following content:
---
# vars file for net_backup
backup_path: backupsEdit the roles/net_backup/tasks/main.yml task file to dynamically include the junos.yml and ios.yml task files.
Use the tree command to display the final role structure and files.
Edit the roles/net_backup/tasks/main.yml task file to dynamically include the junos.yml and ios.yml task files.
The completed file must consist of the following content:
--- # tasks file for net_backup- name: Backup network device configsansible.builtin.include_tasks:file: "{{ ansible_network_os.split('.')[2] }}.yml"
Use the tree command to display the final role structure and files:
[student@workstation simplify-roles]$ tree roles/net_backup
roles/net_backup
├── README.md
├── meta
│ └── main.yml
├── tasks
│ ├── backup_ios.yml
│ ├── backup_junos.yml
│ ├── ios.yml
│ ├── junos.yml
│ ├── junos_netconf_enable.yml
│ └── main.yml
└── vars
└── main.yml
3 directories, 9 filesEdit the roles/net_backup/meta/main.yml file to document your role.
Add your name, a role description, and, optionally, your company name:
galaxy_info:
author: your name
description: your role description
company: your company (optional)
...output omitted...Normally, you should also modify the README.md file to document your role.
That step is skipped in this exercise to save time.
Create a playbook named net_backup.yml that uses the net_backup role, and then run the playbook.
Create a playbook in the /home/student/simplify-roles directory named net_backup.yml that uses the net_backup role.
Add the following content to the playbook:
---
- name: Back up managed network node configurations
hosts:
- junos
- ios
gather_facts: false
tasks:
- name: Backup network managed node configurations
ansible.builtin.include_role:
name: net_backup
when: ansible_network_os is definedRun the net_backup.yml playbook:
[student@workstation simplify-roles]$ ansible-navigator run net_backup.yml
...output omitted...Display the contents of some files generated by the playbook to verify that it was successful, or review them in VS Code.
[student@workstation simplify-roles]$head backups/iosxe1.lab.example.com.cfg...output omitted... [student@workstation simplify-roles]$head backups/junos2.lab.example.com.cfg...output omitted...
Click → in VS Code to close the /home/student/simplify-roles directory, or run the cd command in a GNOME terminal to return to the student home directory:
[student@workstation simplify-roles]$ cd