Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def remove_vlan():
nornir_inventory = gen_inventory_dict("~/nornir_inventory/")
nr = InitNornir(inventory=nornir_inventory, logging=NORNIR_LOGGING)
ex3_hosts = nr.filter(F(groups__contains="eos") | F(groups__contains="nxos"))
ex3_hosts.run(task=networking.netmiko_send_config, config_commands=["no vlan 123"])
def test_explicit_netmiko_connection(self, nornir):
nornir.filter(name="dev4.group_2").run(task=connections.netmiko_connection)
result = nornir.filter(name="dev4.group_2").run(
networking.netmiko_send_config, config_commands="hostname"
)
assert result
for h, r in result.items():
assert h in r.result.strip()
import ipdb
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_config
if __name__ == "__main__":
nr = InitNornir(config_file="config.yaml")
nr = nr.filter(name="nxos1")
# Direct inline configs
commands = ["interface loopback90", "ip address 172.31.90.1/32"]
ipdb.set_trace()
results = nr.run(task=netmiko_send_config, config_commands=commands)
print(results["nxos1"][0].result)
command_string=f"dir flash:/{img}"
)
output = result[0].result
# Drop the first line as that line always contains the filename
output = re.split(r"Directory of.*", output, flags=re.M)[1]
if img not in output:
return False
commands = f"""
default boot system
boot system flash {primary_img}
boot system flash {backup_img}
"""
command_list = commands.strip().splitlines()
task.run(
task=netmiko_send_config,
config_commands=command_list
)
return True
multi_result = task.run(
task=networking.netmiko_send_command,
command_string=f"show vlan brief | i {vlan_id}",
)
# Inspect results and return if already correct
vlan_out = multi_result[0].result
if vlan_out:
existing_vlan_id = vlan_out.split()[0]
existing_vlan_name = vlan_out.split()[1]
if existing_vlan_id == vlan_id and existing_vlan_name == vlan_name:
return "No changes: configuration already correct."
# Configuration not correct - make changes
task.run(
task=networking.netmiko_send_config,
config_commands=[f"vlan {vlan_id}", f"name {vlan_name}"],
)
return "Configuration changed!"
command_string=f"dir flash:/{img}"
)
output = result[0].result
# Drop the first line as that line always contains the filename
output = re.split(r"Directory of.*", output, flags=re.M)[1]
if img not in output:
return False
commands = f"""
default boot system
boot system flash {primary_img}
boot system flash {backup_img}
"""
command_list = commands.strip().splitlines()
task.run(
task=netmiko_send_config,
config_commands=command_list
)
return True
)
# Inspect results and return if already correct
vlan_out = multi_result[0].result
if vlan_out:
existing_vlan_id = vlan_out.split()[0]
existing_vlan_name = vlan_out.split()[1]
if existing_vlan_id == vlan_id and existing_vlan_name == vlan_name:
changed = False
failed = False
result = f"Vlan {vlan_id} with name {vlan_name} exists, nothing to do!"
return Result(host=task.host, result=result, changed=changed, failed=failed)
changed = True
multi_result = task.run(
task=networking.netmiko_send_config,
config_commands=[f"vlan {vlan_id}", f"name {vlan_name}"],
)
if (
"%Invalid command" in multi_result[0].result
or "% Invalid input" in multi_result[0].result
):
failed = True
result_msg = "An invalid configuration command was used."
else:
# Note task still could be marked at failed from the "netmiko_send_config"
# execution i.e. at the MultiResult level.
failed = False
result_msg = f"Configured vlan {vlan_id} with name {vlan_name}!"
return Result(host=task.host, result=result_msg, changed=changed, failed=failed)
def prepare_interfaces(task):
"""
Nornir task to ensure routed interfaces and loopbacks are configured
Not idempotent! Simply sends a list of configurations from the hosts inventory.
Args:
task: nornir task object
"""
task.run(
task=networking.netmiko_send_config, config_commands=task.host["prep_configs"]
)
def set_snmp_id(task):
if task.host.platform == "eos":
snmp_config = [f"snmp chassis-id {task.host['snmp_id']}-{task.host.name}"]
elif task.host.platform == "ios":
snmp_config = [f"snmp-server chassis-id {task.host['snmp_id']}"]
else:
return False
task.run(task=networking.netmiko_send_config, config_commands=snmp_config)
task=networking.netmiko_send_command,
command_string="show route-map | i RM_BGP_",
)
if not route_map.result:
task.run(
task=networking.netmiko_send_config,
config_commands=["route-map RM_BGP_PLACEHOLDER permit 10"],
)
prefix_list = task.run(
task=networking.netmiko_send_command,
command_string="show ip prefix-list | i PL_BGP_",
)
if not prefix_list.result:
task.run(
task=networking.netmiko_send_config,
config_commands=[
"ip prefix-list PL_BGP_PLACEHOLDER seq 5 permit 1.1.1.1/32"
],
)
task.run(
task=networking.napalm_configure,
replace=False,
configuration=f"""
feature bgp
router bgp {task.host['bgp_config']['bgp_asn']}
""",