Troubleshoot a playbook that has been given to you that does not work properly.
Outcomes
Troubleshoot and resolve issues in playbooks.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise, and to ensure that all required resources are available.
This command also creates a project directory with the files needed for the exercise.
[student@workstation ~]$ lab start run-trouble
Instructions
You have been provided with a playbook called vlan_data.yml.
The playbook collects the VLANs configured on the IOS managed nodes, and saves that information in local files.
Each local file is named after the managed node, followed by the _vlan suffix, ending with the yml file extension.
The playbook is not working. Find out what the problem is and fix it.
To use Ansible Lint from Visual Studio Code (VS Code) on the workstation machine, enable the use of the hub.lab.example.com/ee-supported-rhel8:latest automation execution environment.
Open VS Code. Click → and then click in the extension.
Click the icon and then click .
Scroll down to the settings option. Enable the use of an automation execution environment.
Scroll down to the settings option.
Specify hub.lab.example.com/ee-supported-rhel8:latest as the name of the image to use.
Close the and tabs.
Open the /home/student/run-trouble directory in VS Code and review the vlan_data.yml playbook.
Set the Python environment by selecting the recommended interpreter path.
Click → .
Navigate to → and click .
If prompted, select , and then click .
Click the vlan_data.yml playbook.
The playbook runs against the ios inventory group of managed nodes, and has two tasks.
The first task gathers the VLAN information.
The second task saves the collected information in local files.
---
- name: Creating a file with VLANs data
hosts: ios
gather_facts: false
task:
- name: Gather IOS interface facts
cisco.ios.ios_facts:
gather_network_resources: vlans
- name: Copying the vlan configuration to a file
ansible.builtin.copy:
content: "{{ vlans | to_nice_yaml }}"
dest: "{{ inventory_hostname }}_vlan.yml"
mode: 0600Click in the status bar, and then click the recommended option.
![]() |
After setting the Python language, the status bar must display the following information:
The playbook is recognized as an Ansible file.
The automation execution environment is enabled and configured.
The Python language is set.
![]() |
Find and fix the problems in the vlan_data.yml playbook detected by Ansible Lint.
Open the VS Code problems panel to follow the Ansible Lint issues detected.
Click → to open the problems panel in VS Code.
Ansible Lint detects an issue in the play definition. The broken rule is syntax related.
![]() |
Fix the playbook by using tasks instead of task for the play attribute.
Notice how the color that VS Code uses for the tasks attribute is now the same as that used for the other play attributes.
If you hover over the tasks attribute, Ansible Lint displays information about what the tasks attribute is.
![]() |
Ansible Lint provides information about the tasks attribute
Save the playbook. Ansible Lint detects a new issue in the eighth line of the file. The broken rule is related to the indentation in YAML format. Ansible Lint expects eight indentation spaces but it found seven.
![]() |
Fix the playbook by adding one more indentation space on the eighth line. Save the playbook. Ansible Lint no longer detects any issues.
![]() |
Verify that the modified playbook works as expected. If it fails, use automation content navigator to generate a playbook artifact that enables you to perform a deeper analysis.
In a production network, where the playbook has passed the Ansible Lint revisions but does not work as expected, you can share the playbook artifacts with others to assist with troubleshooting.
Switch to the tab in VS Code, or change to the /home/student/run-trouble directory in a GNOME terminal.
The tab in VS Code is in the same panel as the tab.
Use the ansible-navigator run command to run the playbook.
The output shows a fatal result for the second task in the play.
[student@workstation run-trouble]$ansible-navigator run vlan_data.ymlPLAY [Creating a file with VLANs data] *************************************** TASK [Gather IOS interface facts] ******************************************** ok: [iosxe2.lab.example.com] ok: [iosxe1.lab.example.com]TASK[Copying the vlan configuration to a file] *******************************fatal: [iosxe1.lab.example.com]: FAILED! => {"msg": "to_nice_yaml - 'vlans' is undefined. 'vlans' is undefined"}fatal: [iosxe2.lab.example.com]: FAILED! => {"msg": "to_nice_yaml - 'vlans' is undefined. 'vlans' is undefined"} PLAY RECAP ******************************************************************* iosxe1.lab.example.com : ok=1 changed=0 unreachable=0failed=1... iosxe2.lab.example.com : ok=1 changed=0 unreachable=0failed=1... Please review the log for errors.
Run the playbook again.
This time use the --pae true option to generate one artifact file.
[student@workstation run-trouble]$ ansible-navigator run vlan_data.yml --pae trueLocate the generated playbook artifact.
The name of the playbook artifact file starts with the name of the playbook, followed by -artifact-, the time when the playbook was run, and ending with the json file extension.
[student@workstation run-trouble]$ ls vlan_data-artifact*.json
vlan_data-artifact-2023-06-12T04:14:14.286421+00:00.jsonUse the playbook artifact to explore the previous run and troubleshoot the issues found.
Replay the generated playbook artifact using the ansible-navigator replay command with the -m interactive option:
[student@workstation run-trouble]$ansible-navigator replay -m interactive \your-artifact-file-name
The initial replay screen from automation content navigator shows the failed status for the Creating a file with VLANs data play.
Press 0 to display details of the play.
The output shows that the first task was successful in both managed nodes.
As expected, the second task failed on both managed nodes.
![]() |
Press 2 to display the details of the second task in the iosxe1.lab.example.com managed node.
The message indicates that the vlans variable is undefined.
![]() |
Press Esc to go back to the previous page. Because the second task only saves the information obtained in the first task, the defined variable that provides information about the VLANs is in the first task.
Press 0 to inspect the details of the first task in the iosxe1.lab.example.com managed node.
Notice that the ansible_network_resources variable specifies the VLANs for the managed node.
![]() |
Type :q and then press Enter to exit the ansible-navigator command.
Verify that the ansible_network_resources variable provides you with the information you want to save to the local files.
Edit the playbook in VS Code.
Add a task between the two existing tasks, and then save the file.
Use the ansible.builtin.debug module with the var parameter to print the value for the ansible_network_resources variable:
- name: Displaying the variable to debugansible.builtin.debug:var:ansible_network_resources
Notice how as you create the task in VS Code, the autocomplete feature helps you to avoid making mistakes.
![]() |
Go back to the terminal and run the playbook again.
The ansible_network_resources variable effectively provides the required information for both managed nodes.
[student@workstation run-trouble]$ansible-navigator run vlan_data.ymlPLAY [Creating a file with VLANs data] ***************************************** TASK [Gather IOS interface facts] ********************************************** ok: [iosxe1.lab.example.com] ok: [iosxe2.lab.example.com]TASK [Displaying the variable to debug]**************************************ok: [iosxe1.lab.example.com] => { "ansible_network_resources": { "vlans": [ { "mtu": 1500, "name": "default", "shutdown": "disabled", "state": "active", "vlan_id": 1 }, ...output omitted...ok: [iosxe2.lab.example.com] => { "ansible_network_resources": { "vlans": [ { "mtu": 1500, "name": "default", "shutdown": "disabled", "state": "active", "vlan_id": 1 }, ...output omitted... TASK [Copying the vlan configuration to a file] ******************************** fatal: [iosxe1.lab.example.com]: FAILED! => {"msg": "to_nice_yaml - 'vlans' is undefined. 'vlans' is undefined"} fatal: [iosxe2.lab.example.com]: FAILED! => {"msg": "to_nice_yaml - 'vlans' is undefined. 'vlans' is undefined"} PLAY RECAP ********************************************************************* iosxe1.lab.example.com : ok=2 changed=0 unreachable=0 failed=1 ... iosxe2.lab.example.com : ok=2 changed=0 unreachable=0 failed=1 ...
Now that you found what the problem is and the variable you are looking for, fix the playbook. Verify that it works as expected.
Edit the playbook.
Delete the task that you added in the previous step and use the ansible_network_resources variable in the second task, instead of using the undefined vlans variable.
The fixed version of the vlan_data.yml playbook must contain the following content:
---
- name: Creating a file with VLANs data
hosts: ios
gather_facts: false
tasks:
- name: Gather IOS interface facts
cisco.ios.facts:
gather_network_resources: vlans
- name: Copying the vlan configuration to a file
ansible.builtin.copy:
content: "{{ ansible_network_resources | to_nice_yaml }}"
dest: "{{ inventory_hostname }}_vlan.yml"
mode: 0600Run the playbook again.
This time the playbook ends successfully.
The second task has a result of changed which means that the local files have been created.
[student@workstation run-trouble]$ansible-navigator run vlan_data.ymlPLAY [Creating a file with VLANs data] ***************************************** TASK [Gather IOS interface facts] ********************************************** ok: [iosxe2.lab.example.com] ok: [iosxe1.lab.example.com] TASK [Copying the vlan configuration to a file] ********************************changed: [iosxe1.lab.example.com]changed: [iosxe2.lab.example.com] PLAY RECAP ********************************************************************* iosxe1.lab.example.com : ok=2 changed=1 unreachable=0 failed=0 ... iosxe2.lab.example.com : ok=2 changed=1 unreachable=0 failed=0 ...
Find the iosxe1.lab.example.com_vlan.yml and iosxe2.lab.example.com_vlan.yml files in the explorer area of VS Code.
Select either one of them to see the VLAN data.
![]() |
Close the /home/student/run-trouble directory in VS Code.
If you are using the GNOME terminal, return to the /home/student directory.
Click → to close the /home/student/run-trouble directory.
If you are using the GNOME terminal, run the cd command to return to the student home directory:
[student@workstation run-trouble]$ cd