Below is an Ansible playbook to perform routine health checks on network devices
(such as Cisco or Juniper devices) to ensure they are operating optimally. This
script checks parameters like CPU usage, memory utilization, interface status, and
hardware health indicators.
---
### **Network Device Health Check Playbook**
#### **Playbook Code**
```yaml
---
- name: Perform Health Check on Network Devices
hosts: all
gather_facts: no
tasks:
- name: Check CPU usage on Cisco device
[Link].ios_command:
commands:
- show processes cpu sorted
register: cpu_result
- name: Check memory utilization on Cisco device
[Link].ios_command:
commands:
- show memory statistics
register: memory_result
- name: Check interface status on Cisco device
[Link].ios_command:
commands:
- show interfaces status
register: interface_result
- name: Check hardware health indicators on Cisco device
[Link].ios_command:
commands:
- show environment
register: hardware_result
- name: Print CPU usage result
debug:
var: cpu_result
- name: Print memory utilization result
debug:
var: memory_result
- name: Print interface status result
debug:
var: interface_result
- name: Print hardware health result
debug:
var: hardware_result
- name: Check CPU usage on Juniper device
[Link].junos_command:
commands:
- show system processes extensive
register: juniper_cpu_result
- name: Check memory utilization on Juniper device
[Link].junos_command:
commands:
- show system memory
register: juniper_memory_result
- name: Check interface status on Juniper device
[Link].junos_command:
commands:
- show interfaces terse
register: juniper_interface_result
- name: Check hardware health indicators on Juniper device
[Link].junos_command:
commands:
- show chassis hardware
register: juniper_hardware_result
- name: Print Juniper CPU usage result
debug:
var: juniper_cpu_result
- name: Print Juniper memory utilization result
debug:
var: juniper_memory_result
- name: Print Juniper interface status result
debug:
var: juniper_interface_result
- name: Print Juniper hardware health result
debug:
var: juniper_hardware_result
```
---
### **Explanation**
#### **Header and Setup**
1. **`---`**
Marks the beginning of the YAML file.
2. **`- name: Perform Health Check on Network Devices`**
This is the name of the play, indicating its purpose: performing health checks
on the network devices.
3. **`hosts: all`**
Specifies that the playbook should run on all the hosts in the inventory. These
can be Cisco and Juniper devices as defined in the inventory file.
4. **`gather_facts: no`**
Skips the fact gathering process, as it isn't necessary for this health check
task.
---
#### **Tasks for Cisco Devices**
5. **`- name: Check CPU usage on Cisco device`**
This task checks the CPU usage of the Cisco device.
6. **`[Link].ios_command:`**
Uses the `ios_command` module from the `[Link]` collection to run a command
on the Cisco device.
7. **`commands:`**
Specifies the command to be executed on the Cisco device. In this case, the
`show processes cpu sorted` command displays CPU utilization statistics.
8. **`register: cpu_result`**
Captures the output of the command in the `cpu_result` variable for later use.
9. **`- name: Check memory utilization on Cisco device`**
This task checks the memory utilization of the Cisco device.
10. **`commands:`**
Runs the command `show memory statistics`, which shows memory utilization on
the device.
11. **`register: memory_result`**
Captures the output of the memory check in the `memory_result` variable.
12. **`- name: Check interface status on Cisco device`**
This task checks the status of the interfaces on the Cisco device.
13. **`commands:`**
Runs the `show interfaces status` command to get interface status information
(up/down, administrative state).
14. **`register: interface_result`**
Captures the result of the interface status command in the `interface_result`
variable.
15. **`- name: Check hardware health indicators on Cisco device`**
This task checks the hardware health status of the Cisco device.
16. **`commands:`**
Runs the `show environment` command, which provides information about hardware
health such as temperature, fan status, and power supply.
17. **`register: hardware_result`**
Captures the result of the hardware health check in the `hardware_result`
variable.
18. **`- name: Print CPU usage result`**
This debug task prints the output of the CPU usage check to the console for
review.
19. **`debug:`**
The `debug` module is used to print variables for troubleshooting and
reporting.
20. **`var: cpu_result`**
Specifies the variable to be printed (`cpu_result`).
21. **`- name: Print memory utilization result`**
This debug task prints the output of the memory utilization check.
22. **`var: memory_result`**
Specifies the variable to be printed (`memory_result`).
23. **`- name: Print interface status result`**
This debug task prints the output of the interface status check.
24. **`var: interface_result`**
Specifies the variable to be printed (`interface_result`).
25. **`- name: Print hardware health result`**
This debug task prints the output of the hardware health check.
26. **`var: hardware_result`**
Specifies the variable to be printed (`hardware_result`).
---
#### **Tasks for Juniper Devices**
27. **`- name: Check CPU usage on Juniper device`**
This task checks the CPU usage on the Juniper device.
28. **`[Link].junos_command:`**
Uses the `junos_command` module from the `[Link]` collection to
execute commands on Juniper devices.
29. **`commands:`**
Specifies the command `show system processes extensive`, which displays CPU
statistics.
30. **`register: juniper_cpu_result`**
Captures the result of the command into the `juniper_cpu_result` variable.
31. **`- name: Check memory utilization on Juniper device`**
This task checks memory utilization on the Juniper device.
32. **`commands:`**
Runs the `show system memory` command to check memory usage.
33. **`register: juniper_memory_result`**
Captures the memory result in the `juniper_memory_result` variable.
34. **`- name: Check interface status on Juniper device`**
This task checks the interface status on the Juniper device.
35. **`commands:`**
Runs the `show interfaces terse` command, which provides concise information on
interface statuses.
36. **`register: juniper_interface_result`**
Captures the interface status result in the `juniper_interface_result`
variable.
37. **`- name: Check hardware health indicators on Juniper device`**
This task checks the hardware health on the Juniper device.
38. **`commands:`**
Runs the `show chassis hardware` command, which displays hardware health
indicators like the status of physical components (e.g., fans, temperature).
39. **`register: juniper_hardware_result`**
Captures the result of the hardware health check in the
`juniper_hardware_result` variable.
40. **`- name: Print Juniper CPU usage result`**
This debug task prints the output of the CPU usage check for the Juniper
device.
41. **`debug:`**
The `debug` module is used again to display the variable content.
42. **`var: juniper_cpu_result`**
Specifies the variable to be printed (`juniper_cpu_result`).
43. **`- name: Print Juniper memory utilization result`**
This debug task prints the output of the memory utilization check for the
Juniper device.
44. **`var: juniper_memory_result`**
Specifies the variable to be printed (`juniper_memory_result`).
45. **`- name: Print Juniper interface status result`**
This debug task prints the output of the interface status check for the Juniper
device.
46. **`var: juniper_interface_result`**
Specifies the variable to be printed (`juniper_interface_result`).
47. **`- name: Print Juniper hardware health result`**
This debug task prints the output of the hardware health check for the Juniper
device.
48. **`var: juniper_hardware_result`**
Specifies the variable to be printed (`juniper_hardware_result`).
---
### **Prerequisites**
1. **Install Required Collections**
Install the necessary Ansible collections for Cisco and Juniper devices:
```bash
ansible-galaxy collection install [Link] [Link]
```
2. **Define Inventory File**
Create an inventory file with your Cisco and Juniper devices. Example
(`[Link]`):
```yaml
cisco_devices:
hosts:
cisco_router:
ansible_host: [Link]
ansible_user: admin
ansible_password: admin
ansible_network_os: [Link]
juniper_devices:
hosts:
juniper_router:
ansible_host: [Link]
ansible_user: admin
ansible_password: admin
ansible_network_os: [Link]
```
3. **Run the Playbook**
Run the playbook to perform health checks:
```bash
ansible-playbook -i [Link] network_health_check.yml
```
---
### **Results**
- The playbook will output the CPU usage, memory utilization, interface statuses,
and hardware health for each device.
- It helps engineers proactively monitor device health and resolve issues before
they impact the network.