Here’s an Ansible playbook to automate common troubleshooting procedures for
network issues. This playbook performs tasks like checking device connectivity,
pinging remote hosts, tracing routes, and verifying configuration settings.
---
### **Ansible Playbook for Network Troubleshooting**
#### **Playbook Code**
```yaml
---
- name: Automate Network Troubleshooting Procedures
  hosts: all
  gather_facts: no
  tasks:
    - name: Verify device connectivity
      ping:
      - name: Check remote host reachability from the network device
        cisco.ios.ios_command:
          commands:
            - ping 8.8.8.8
        register: ping_result
      - name: Trace route to a remote host
        cisco.ios.ios_command:
          commands:
            - traceroute 8.8.8.8
        register: traceroute_result
      - name: Verify interface configurations
        cisco.ios.ios_command:
          commands:
            - show running-config interface
        register: interface_config
      - name: Verify routing table
        cisco.ios.ios_command:
          commands:
            - show ip route
        register: routing_table
      - name: Check CPU usage
        cisco.ios.ios_command:
          commands:
            - show processes cpu sorted
        register: cpu_usage
      - name: Check memory utilization
        cisco.ios.ios_command:
          commands:
            - show memory statistics
        register: memory_usage
      - name: Display ping results
        debug:
          var: ping_result.stdout
      - name: Display traceroute results
        debug:
          var: traceroute_result.stdout
      - name: Display interface configurations
        debug:
          var: interface_config.stdout
      - name: Display routing table
        debug:
          var: routing_table.stdout
      - name: Display CPU usage
        debug:
          var: cpu_usage.stdout
      - name: Display memory utilization
        debug:
          var: memory_usage.stdout
```
---
### **Explanation**
#### **Header Section**
1. **`---`**
   Marks the beginning of the YAML file.
2. **`- name: Automate Network Troubleshooting Procedures`**
   Describes the purpose of this playbook: automating network troubleshooting
tasks.
3. **`hosts: all`**
   Specifies that the playbook applies to all devices listed in the inventory file.
4. **`gather_facts: no`**
   Skips fact gathering since it is not required for troubleshooting tasks.
---
#### **Tasks**
5. **`- name: Verify device connectivity`**
   Tests if the Ansible control node can connect to the target devices.
6. **`ping:`**
   The built-in `ping` module ensures the devices in the inventory are reachable.
---
7. **`- name: Check remote host reachability from the network device`**
   Pings a remote host (e.g., `8.8.8.8`) from the network device.
8. **`cisco.ios.ios_command:`**
   Uses the `ios_command` module from the `cisco.ios` collection to run the `ping`
command on Cisco devices.
9. **`commands:`**
   Specifies the command to execute on the Cisco device, here `ping 8.8.8.8`.
10. **`register: ping_result`**
    Saves the output of the ping command into the `ping_result` variable.
---
11. **`- name: Trace route to a remote host`**
    Traces the path to a remote host to diagnose routing issues.
12. **`commands:`**
    Specifies the `traceroute 8.8.8.8` command to trace the path to the specified
IP.
13. **`register: traceroute_result`**
    Saves the traceroute command output in the `traceroute_result` variable.
---
14. **`- name: Verify interface configurations`**
    Fetches the current running configuration for all interfaces.
15. **`commands:`**
    Uses the `show running-config interface` command to display interface
configurations.
16. **`register: interface_config`**
    Stores the command output in the `interface_config` variable.
---
17. **`- name: Verify routing table`**
    Checks the device’s routing table to diagnose routing issues.
18. **`commands:`**
    Runs the `show ip route` command to display the routing table.
19. **`register: routing_table`**
    Captures the output of the routing table command in the `routing_table`
variable.
---
20. **`- name: Check CPU usage`**
    Fetches CPU usage information to identify performance issues.
21. **`commands:`**
    Uses the `show processes cpu sorted` command to display CPU utilization data.
22. **`register: cpu_usage`**
    Stores the command output in the `cpu_usage` variable.
---
23. **`- name: Check memory utilization`**
    Retrieves memory utilization statistics.
24. **`commands:`**
    Runs the `show memory statistics` command to get details about memory usage.
25. **`register: memory_usage`**
    Captures the memory statistics output in the `memory_usage` variable.
---
#### **Debugging Results**
26. **`- name: Display ping results`**
    Prints the ping output for review.
27. **`debug:`**
    The `debug` module is used to display the variable content in the console.
28. **`var: ping_result.stdout`**
    Specifies that only the `stdout` (standard output) of the `ping_result`
variable should be printed.
29. **Subsequent Debug Tasks**
    - Similar `debug` tasks are used to display the outputs of other variables,
such as `traceroute_result`, `interface_config`, `routing_table`, `cpu_usage`, and
`memory_usage`.
---
### **Prerequisites**
1. **Install Cisco Collection**
   Ensure the `cisco.ios` collection is installed:
   ```bash
   ansible-galaxy collection install cisco.ios
   ```
2. **Inventory File Example (`inventory.yml`)**
   Define your devices in an inventory file:
   ```yaml
   all:
     hosts:
        cisco_router:
          ansible_host: 192.168.1.1
          ansible_user: admin
          ansible_password: admin
          ansible_network_os: cisco.ios
   ```
3. **Run the Playbook**
   Execute the playbook:
   ```bash
   ansible-playbook -i inventory.yml network_troubleshooting.yml
   ```
---
### **Use Cases**
- Diagnose connectivity issues between devices and remote hosts.
- Trace routing paths to detect possible misconfigurations or network loops.
- Analyze interface configurations and routing tables for discrepancies.
- Check system resource usage (CPU and memory) for performance bottlenecks.
This playbook provides an efficient, repeatable method for identifying and
resolving network issues quickly.