We will be using passwordless ssh access to the switch, so ssh-rsa key has to be configured on the target switch.
All we need to do here is to ssh to the switch, run the command: "show interfaces vlan terse | match inet" and get vlan interface IDs from the output.
If the switch has vlan interfaces configured, the output will look like this:
vlan.501upup inet 10.5.5.10/24
vlan.601upup inet 10.6.6.10/24
The following python regex will be used to get vlan interface numbers:
pulled_vlans = re.findall(r'vlan.(\d{3})', cli_output)
We need to write a simple custom Python ansible module that will be called from the playbook.
**TIP: To make a custom module available to Ansible, you can either specify the path to your custom module in an environment variable, ANSIBLE_LIBRARY; use the --module-path command-line option, or drop the modules in a ./library directory alongside your top-level playbooks.
I chose to follow the last option.
Here is module library/get_vlan_iface:
Next - we will create an inventory file network_inventory.corp:
and a playbook file get_swi.yml:
To tset this module, we can use test-module script:
And finally, we run the playbook: