- Network Automation Cookbook
- Karim Okasha
- 292字
- 2025-04-04 13:10:14
How it works...
In this playbook, we are using the ios_ping module, which logs into each node defined in our Ansible inventory, and pings the destination specified by the dest attribute. In this sample playbook, we would like to validate network reachability to a single host within the data, voice, and web VLANs, and we choose the tenth host in all these VLANs (just as an example). In order to build all the VLAN prefixes we set in the first task, we add a new variable called all_svi_prefixes and use multiple jinja2 filters to collect only those prefixes that are running VRRP (so as to remove any core VLANs). We get only the IPv4 attributes for these SVI interfaces. The following are the contents of this new variable after running the first task:
ok: [core01 -> localhost] => {
"all_svi_prefixes": [
"10.1.10.0/24",
"10.1.20.0/24",
"10.1.100.0/24"
]
}
We supply this new list data structure to the ios_ping module and we specify that we need to ping the tenth host within each subnet. As long as the ping succeeds, the task will succeed. However, if there is a connectivity problem from the router/switch to this host, the task will fail. We are using the ignore_errors parameter in order to ignore any failure that might occur owing to the fact that the host is unreachable/down, and to run any subsequent tasks. The following code snippet outlines the successful run:
TASK [P1T2: Ping Hosts in all VLANs] *****************************
ok: [core01] => (item=10.1.10.0/24)
ok: [core02] => (item=10.1.10.0/24)
ok: [wan01] => (item=10.1.10.0/24)
ok: [wan02] => (item=10.1.10.0/24)
ok: [core01] => (item=10.1.20.0/24)
ok: [core02] => (item=10.1.20.0/24)
ok: [core01] => (item=10.1.100.0/24)
ok: [wan01] => (item=10.1.20.0/24)
ok: [wan02] => (item=10.1.20.0/24)
ok: [core02] => (item=10.1.100.0/24)
ok: [wan01] => (item=10.1.100.0/24)
ok: [wan02] => (item=10.1.100.0/24)