Include and import playbooks and tasks for different network vendors in a top-level Ansible Playbook.
Outcomes
Create a playbook that uses conditionals to select the appropriate task file.
Create task files to encourage reusing code.
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-files
Instructions
Open the /home/student/simplify-files directory in VS Code and create the backup.yml playbook to create backup files of the managed nodes in the junos and ios inventory groups.
Open VS Code and then click → .
Navigate to → and click .
If prompted, select , and then click .
Create the backup.yml file and target the managed nodes in the junos and ios inventory groups.
Do not gather facts for the managed nodes.
Add the following lines to the playbook:
---- name: Back up all managed network nodeshosts:- junos- iosgather_facts: false![]()
tasks:
Add a block to the bottom of the play that targets the Juniper Junos managed nodes. Add two tasks to include the specified task files:
---
- name: Back up all managed network nodes
hosts:
- junos
- ios
gather_facts: false
tasks:
- name: Juniper Junos block
when: ansible_network_os == 'junipernetworks.junos.junos'
block:
- name: Enable NETCONF
ansible.builtin.include_tasks: tasks/junos_netconf_enable.yml
- name: Create Junos backup files
ansible.builtin.include_tasks: tasks/junos_backup_create.yml 
Create the tasks directory and the task files specified in the backup.yml playbook.
Switch to the tab in VS Code, or change to the /home/student/simplify-files directory in a GNOME terminal:
[student@workstation ~] cd ~/simplify-filesUse the mkdir command to create the tasks directory:
[student@workstation simplify-files]$ mkdir tasksCreate the tasks/junos_netconf_enable.yml task file with the following content:
---
- name: Enable NETCONF service
vars:
ansible_connection: ansible.netcommon.network_cli
junipernetworks.junos.junos_netconf:
netconf_port: 830
state: presentCreate the tasks/junos_backup_create.yml task file with the following content:
--- - name: Create the backup file junipernetworks.junos.junos_config:backup: true register: create_file - name: Show create_file ansible.builtin.debug: var: create_file
This task uses the | |
If you register the output of the |
Run the backup.yml playbook.
Using the information displayed in the Show create_file task, update the tasks/junos_backp_create.yml task file to create a symbolic link that points to the latest backup file.
Run the backup.yml playbook and examine the output of the Show create_file task:
[student@workstation simplify-files]$ansible-navigator run backup.yml...output omitted... TASK [Show create_file] ******************************************************** ok: [junos1.lab.example.com] => { "create_file": { "backup_path": "/home/student/simplify-files/backup/junos1.lab.example.com_config.2023-08-16@14:12:46","changed": true, "date": "2023-08-16", "failed": false, "filename": "junos1.lab.example.com_config.2023-08-16@14:12:46",
"shortname": "/home/student/simplify-files/backup/junos1.lab.example.com_config",
"time": "14:12:46" } } ok: [junos2.lab.example.com] => { "create_file": { "backup_path": "/home/student/simplify-files/backup/junos2.lab.example.com_config.2023-08-16@14:12:46", "changed": true, "date": "2023-08-16", "failed": false, "filename": "junos2.lab.example.com_config.2023-08-16@14:12:46", "shortname": "/home/student/simplify-files/backup/junos2.lab.example.com_config", "time": "14:12:46" } } ...output omitted...
The value of the | |
The value of the | |
The value of the |
Update the tasks/junos_backup_create.yml task file.
Add a task that creates a symbolic link to the most recent backup file.
You might also update the Show create_file task so that the task is skipped unless you run the playbook in verbose mode:
---
- name: Create the backup file
junipernetworks.junos.junos_config:
backup: true
register: create_file
- name: Show create_file
ansible.builtin.debug:
var: create_file
verbosity: 1
- name: Create or update the 'latest' link
ansible.builtin.file:
path: "{{ create_file['shortname'] }}.latest"
src: "{{ create_file['filename'] }}"
state: link
force: true 
Skip this task unless you run the playbook with at least one | |
Create the symbolic link by appending | |
The task creates a symbolic link that uses a relative path to a file in the same directory as the link.
(Optional) Use the value of the | |
Create a symbolic link. | |
Because this example uses a relative path, you must force the creation of the symbolic link. |
Run the backup.yml playbook:
[student@workstation simplify-files]$ ansible-navigator run backup.yml
...output omitted...
TASK [Create or update the 'latest' link] **************************************
changed: [junos2.lab.example.com]
changed: [junos1.lab.example.com]
PLAY RECAP *********************************************************************
iosxe1.lab.example.com : ok=0 changed=0 unreachable=0 failed=0 ...
iosxe2.lab.example.com : ok=0 changed=0 unreachable=0 failed=0 ...
junos1.lab.example.com : ok=6 changed=2 unreachable=0 failed=0 ...
junos2.lab.example.com : ok=6 changed=2 unreachable=0 failed=0 ...Use the tree command to verify the creation of the symbolic links.
The date and time stamps displayed in this example differ from your output.
[student@workstation simplify-files]$tree backupbackup ├── junos1.lab.example.com_config.2023-08-16@14:12:46 ├── junos1.lab.example.com_config.2023-08-16@14:16:16 ├──junos1.lab.example.com_config.latest -> junos1.lab.example.com_config.2023-08-16@14:16:16├── junos2.lab.example.com_config.2023-08-16@14:12:46 ├── junos2.lab.example.com_config.2023-08-16@14:16:17 └──junos2.lab.example.com_config.latest -> junos2.lab.example.com_config.2023-08-16@14:16:170 directories, 6 files
Update the backup.yml playbook so that it creates backup files for the Cisco IOS managed nodes.
Add a block to the bottom of the play that targets the Cisco IOS managed nodes.
Add a task to include the tasks/ios_backup_create.yml task file.
The finished playbook contains the following content:
---
- name: Back up all managed network nodes
hosts:
- junos
- ios
gather_facts: false
tasks:
- name: Juniper Junos block
when: ansible_network_os == 'junipernetworks.junos.junos'
block:
- name: Enable NETCONF
ansible.builtin.include_tasks: tasks/junos_netconf_enable.yml
- name: Create Junos backup files
ansible.builtin.include_tasks: tasks/junos_backup_create.yml
- name: Cicso IOS block
when: ansible_network_os == 'cisco.ios.ios'
block:
- name: Create IOS backup files
ansible.builtin.include_tasks: tasks/ios_backup_create.yml 
Although this playbook only has one task for the Cisco IOS managed nodes, you might still use a block in case you decide to add more tasks. | |
This task file is almost the same as the |
Because the playbook does not perform loops or pass variables to the task files, you could use the ansible.builtin.import_tasks module instead of the ansible.builtin.include_tasks module.
Use VS Code or a GNOME terminal to copy the tasks/junos_backup_create.yml task file to the tasks/ios_backup_create.yml task file:
[student@workstation simplify-files]$cp tasks/junos_backup_create.yml \tasks/ios_backup_create.yml
Update the tasks/ios_backup_create.yml task file so that it uses the cisco.ios.ios_config module instead of the junipernetworks.junos.junos_config module.
The updated task file contains the following content:
---
- name: Create the backup file
cisco.ios.ios_config:
backup: true
register: create_file
- name: Show create_file
ansible.builtin.debug:
var: create_file
verbosity: 1
- name: Create or update the 'latest' link
ansible.builtin.file:
path: "{{ create_file['shortname'] }}.latest"
src: "{{ create_file['filename'] }}"
state: link
force: trueRun the backup.yml playbook and verify that the playbook creates symbolic links for both the Juniper Junos and Cisco IOS managed nodes.
Run the backup.yml playbook:
[student@workstation simplify-files]$ ansible-navigator run backup.yml
...output omitted...Use the tree command to verify the creation of the symbolic links.
The date and time stamps displayed in this example differ from your output.
Notice that even though the Junos managed nodes have three backup files each, the symbolic links point to the most recent files.
[student@workstation simplify-files]$tree backupbackup ├── iosxe1.lab.example.com_config.2023-08-16@14:21:56 ├──iosxe1.lab.example.com_config.latest -> iosxe1.lab.example.com_config.2023-08-16@14:21:56├── iosxe2.lab.example.com_config.2023-08-16@14:21:56 ├──iosxe2.lab.example.com_config.latest -> iosxe2.lab.example.com_config.2023-08-16@14:21:56├── junos1.lab.example.com_config.2023-08-16@14:12:46 ├── junos1.lab.example.com_config.2023-08-16@14:16:16 ├── junos1.lab.example.com_config.2023-08-16@14:21:51 ├──junos1.lab.example.com_config.latest -> junos1.lab.example.com_config.2023-08-16@14:21:51├── junos2.lab.example.com_config.2023-08-16@14:12:46 ├── junos2.lab.example.com_config.2023-08-16@14:16:17 ├── junos2.lab.example.com_config.2023-08-16@14:21:51 └──junos2.lab.example.com_config.latest -> junos2.lab.example.com_config.2023-08-16@14:21:510 directories, 12 files
Close the /home/student/simplify-files directory in VS Code, or run the cd command in a GNOME terminal to return to the student home directory.
[student@workstation simplify-files]$ cd