Advertisement
Guest User

Untitled

a guest
Mar 9th, 2020
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import logging
  3. from pathlib import Path
  4.  
  5. from nornir import InitNornir
  6. from nornir.plugins.tasks import networking, text
  7. from nornir.plugins.tasks.data import load_yaml
  8. from nornir.plugins.functions.text import print_title, print_result
  9. from nornir.core.filter import F
  10.  
  11. # import ipdb; ipdb.set_trace()
  12.  
  13.  
  14. def configure_bgp(task):
  15.     if Path(f'inventory/bgp/{task.host.name}.yaml').is_file():
  16.         # Load additional data (interfaces)
  17.         data = task.run(
  18.             task=load_yaml,
  19.             file=f'inventory/bgp/{task.host.name}.yaml',
  20.             severity_level=logging.DEBUG,
  21.         )
  22.  
  23.         # Check for route-maps
  24.         for item in data.result['bgp']['address_families']:
  25.             for neighbor in item['neighbors']:
  26.                 if 'route_maps' in neighbor:
  27.                     for rm in neighbor['route_maps']:
  28.                         task.run(
  29.                             task=configure_route_map,
  30.                             data=rm,
  31.                         )
  32.  
  33.         # Transform inventory data to configuration via a template file
  34.         template_config = task.run(
  35.             task=text.template_file,
  36.             name="BGP Configuration",
  37.             template="bgp.j2",
  38.             path=f"templates/{task.host.platform}",
  39.             bgp=data.result['bgp'],
  40.             severity_level=logging.DEBUG,
  41.         )
  42.  
  43.         # Save the compiled configuration into a host variable
  44.         task.host["config"] = template_config.result
  45.  
  46.         # Deploy that configuration to the device using NAPALM
  47.         task.run(
  48.             task=networking.napalm_configure,
  49.             name="Loading Configuration on the device",
  50.             configuration=task.host["config"],
  51.             replace=False,
  52.             severity_level=logging.INFO,
  53.         )
  54.     else:
  55.         print(f'No BGP data found for {task.host.name}!')
  56.  
  57.  
  58. def configure_route_map(task, data):
  59.     # Transform inventory data to configuration via a template file
  60.     template_config = task.run(
  61.         task=text.template_file,
  62.         name=f"ROUTE-MAP '{data['name']}' Configuration",
  63.         template=f"{data['name']}.j2".lower(),
  64.         path=f"templates/{task.host.platform}/route-maps",
  65.         route_map=data,
  66.         severity_level=logging.DEBUG,
  67.     )
  68.  
  69.     # Save the compiled configuration into a host variable
  70.     task.host["config"] = template_config.result
  71.  
  72.     # Deploy that configuration to the device using NAPALM
  73.     task.run(
  74.         task=networking.napalm_configure,
  75.         name="Loading Configuration on the device",
  76.         configuration=task.host["config"],
  77.         replace=False,
  78.         severity_level=logging.INFO,
  79.     )
  80.  
  81.  
  82. def main():
  83.     nr = InitNornir(
  84.         config_file="config.yaml",
  85.         # dry_run=True,
  86.         # core={"num_workers": 1},
  87.     )
  88.  
  89.     devices = nr.filter(
  90.         F(groups__contains="mpls-lab") & ~F(role="switch"),
  91.     )
  92.  
  93.     print_title(f'Hello, my name is title')
  94.  
  95.     r = devices.run(
  96.         task=configure_bgp,
  97.     )
  98.     print_result(r)
  99.  
  100.  
  101. if __name__ == "__main__":
  102.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement