Write and use a playbook containing multiple plays.
Outcomes
Create a playbook with multiple plays to back up the configurations of Cisco IOS XE and Juniper Junos devices.
As the student user on the workstation machine, use the lab command to prepare
your environment for this exercise, and to ensure that all required resources are available.
[student@workstation ~]$ lab start implement-plays
Instructions
Open the ~/implement-plays directory in VS Code and create the inventory file.
Add the Cisco IOS XE and Juniper Junos managed nodes, and the all, ios, and junos groups and variables to the inventory file.
Add the iosxe1.lab.example.com, iosxe2.lab.example.com, junos1.lab.example.com and junos2.lab.example.com devices to the inventory file.
iosxe1.lab.example.com iosxe2.lab.example.com junos1.lab.example.com junos2.lab.example.com
Add the all, ios, and junos groups to the inventory file.
[all] iosxe1.lab.example.com iosxe2.lab.example.com junos1.lab.example.com junos2.lab.example.com [ios] iosxe1.lab.example.com iosxe2.lab.example.com [junos] junos1.lab.example.com junos2.lab.example.com
Add the inventory variables to the inventory file.
The final inventory file must contain the following content:
[all] iosxe1.lab.example.com iosxe2.lab.example.com junos1.lab.example.com junos2.lab.example.com [ios] iosxe1.lab.example.com iosxe2.lab.example.com [junos] junos1.lab.example.com junos2.lab.example.com [all:vars] ansible_user=student ansible_ssh_private_key_file=~/.ssh/lab_rsa [ios:vars] ansible_connection=ansible.netcommon.network_cli ansible_network_os=cisco.ios.ios [junos:vars] ansible_connection=ansible.netcommon.netconf ansible_network_os=junipernetworks.junos.junos
In VS Code, create a playbook called backupconfigs.yml.
Add a play to back up the configuration on the Cisco IOS XE managed nodes in the ios inventory group.
In VS Code, create a playbook called backupconfigs.yml.
Add a play named "Backup IOS XE configurations".
Set the hosts: keyword to ios and the gather_facts keyword to false.
--- - name: Backup IOS XE configurations hosts: ios gather_facts: false tasks:
Add a task named "Backup IOS XE configurations" to back up the configuration on the Cisco IOS XE managed nodes in the ios inventory group.
Use the {{ inventory_hostname }} variable to name the backup file in the format.inventory_hostname.txt
- name: Backup IOS XE configurations
cisco.ios.ios_config:
backup: true
backup_options:
dir_path: /home/student/implement-plays
filename: "{{ inventory_hostname }}.txt"Add a play to back up the configuration on the Juniper Junos managed nodes in the junos inventory group.
Add a play named "Backup Juniper Junos configurations".
Set the hosts: keyword to junos and the gather_facts keyword to false.
- name: Backup Juniper Junos configurations hosts: junos gather_facts: false tasks:
Add a task named "Enable the netconf service on port 830" to enable the netconf service by using the junipernetworks.junos.junos_netconf module.
Set the ansible_connection variable to ansible.netcommon.network_cli to override the value set in the inventory file.
The junipernetworks.junos.junos_config module requires that the netconf service be enabled on the managed Junos nodes.
- name: Enable the netconf service on port 830
vars:
ansible_connection: ansible.netcommon.network_cli
junipernetworks.junos.junos_netconf:
netconf_port: 830
state: presentAdd another task named "Backup Junos configurations" to back up the configuration by using the junipernetworks.junos.junos_config module.
Use the {{ inventory_hostname }} variable to name the backup file in the format.inventory_hostname.txt
This task uses the value of the ansible_connection variable set in the inventory file, ansible.netcommon.netconf.
- name: Backup Junos configurations
junipernetworks.junos.junos_config:
backup: true
backup_options:
dir_path: /home/student/implement-plays
filename: "{{ inventory_hostname }}.txt"The completed playbook must contain the following content:
---
- name: Backup IOS XE configurations
hosts: ios
gather_facts: false
tasks:
- name: Backup IOS XE configurations
cisco.ios.ios_config:
backup: true
backup_options:
dir_path: /home/student/implement-plays
filename: "{{ inventory_hostname }}.txt"
- name: Backup Juniper Junos configurations
hosts: junos
gather_facts: false
tasks:
- name: Enable the netconf service on port 830
vars:
ansible_connection: ansible.netcommon.network_cli
junipernetworks.junos.junos_netconf:
netconf_port: 830
state: present
- name: Backup Junos configurations
junipernetworks.junos.junos_config:
backup: true
backup_options:
dir_path: /home/student/implement-plays
filename: "{{ inventory_hostname }}.txt"Save the file.
Open a terminal and use the ansible-navigator run command to run the playbook:
[student@workstation implement-plays]$ ansible-navigator run \
backupconfigs.yml -m stdout
PLAY [Backup IOS XE configurations] *******************************************
TASK [Backup IOS XE configurations] *******************************************
changed: [iosxe1.lab.example.com]
changed: [iosxe2.lab.example.com]
PLAY [Backup Juniper Junos configurations] ************************************
TASK [Enable the netconf service on port 830] *********************************
changed: [junos1.lab.example.com]
TASK [Backup Junos configurations] ********************************************
changed: [junos1.lab.example.com]
PLAY RECAP ********************************************************************
iosxe1.lab.example.com : ok=1 changed=1 unreachable=0 failed=0 ...
iosxe2.lab.example.com : ok=1 changed=1 unreachable=0 failed=0 ...
junos1.lab.example.com : ok=2 changed=2 unreachable=0 failed=0 ...Verify that the configuration backups were successful:
[student@workstation implement-plays]$ ls *.txt
iosxe1.lab.example.com.txt iosxe2.lab.example.com.txt junos1.lab.example.com.txtClose the /home/student/implement-plays directory in VS Code.
If you are using the GNOME terminal, return to the /home/student directory.
Click → in VS Code to close the /home/student/implement-plays directory.
If you are using the GNOME terminal, run the cd command to return to the student home directory:
[student@workstation implement-plays]$ cd