Adds a VLAN and allows it on the uplink trunk, in a change block with its rollback; run with --dry.
Adds VLAN 30 to each switch in devices.csv and allows it on the uplink trunk named in the
trunk column. It reads show vlan brief first and leaves alone a switch that has the VLAN
already. The change is a change block: its rollback lines are sent if a verify line is
false, if the device refuses a line, or if the program is stopped. A change block with no
rollback is refused before the program runs.
Run it with --dry: each change is printed and nothing is sent. --replay saved answers the
read-only show vlan brief from the saved outputs, so no device is contacted at all.
bogo run --dry --replay saved change_vlan.bogo
bogo test change_vlan.bogo
To use it on your own network:
Put your switches in devices.csv, with the uplink of each in the trunk column. A device
with the role router is skipped.
Set vid and name at the top of the class.
Store a login once with bogo login default.
Run it with --dry and without --replay first: it then really reads each switch, and
prints the change without sending it.
Without --dry and without --replay the change is really sent. Before each change
Bogolang takes a backup of the running configuration.
Check the lines against the software release of your switches before a real run.
change_vlan.bogo
edition2027"""Add a VLAN to each switch and allow it on the uplink trunk, with a rollback for every line."""classChangeVlan:
"""Adds VLAN 30 where it is missing. Read-only first: a switch that has it already is left alone.
Run it with --dry first: each change is printed and nothing is sent."""
vid: int = 30
name: str = "CAMERAS" @staticmethoddef has_vlan(vlans: list[Row], vid: int) -> bool:
"""True when the table of show vlan brief holds this VLAN."""return any(v.vlan_id == str(vid) for v in vlans)
@staticmethoddef main(args: list[str]) -> None:
vid: int = ChangeVlan.vid
name: str = ChangeVlan.name
switches: list[Device] = [d for d in inventory("devices.csv") if d.role != "router"]
for d in switches:
with ssh(d) as s:
vlans: list[Row] = s.show("vlan brief").table()
ifChangeVlan.has_vlan(vlans, vid):
print(f"{d.name}: VLAN {vid} is already there, nothing to do")
continue
trunk: str = d.extra["trunk"]
changeon d:
apply:
vlan {vid}
name {name}
interface {trunk}
switchport trunk allowed vlan add {vid}
rollback:
interface {trunk}
switchport trunk allowed vlan remove {vid}
no vlan {vid}
verify:
name in show("vlan brief")
str(vid) in show(f"interfaces {trunk} trunk")
done: str = "would be added, nothing was sent"if dry_run() else"added"
print(f"{d.name}: VLAN {vid} {done}")
classTestChangeVlan:
def vlans(self) -> list[Row]:
return [
Row({"vlan_id": "1", "vlan_name": "default", "status": "active", "interfaces": ""}),
Row({"vlan_id": "10", "vlan_name": "USERS", "status": "active", "interfaces": ""}),
Row({"vlan_id": "300", "vlan_name": "LAB", "status": "active", "interfaces": ""}),
]
def test_vlan_present(self) -> None:
assertChangeVlan.has_vlan(self.vlans(), 10)
def test_vlan_missing_is_not_matched_by_a_longer_number(self) -> None:
assertnotChangeVlan.has_vlan(self.vlans(), 30)