def netbox(self): if self._netbox is None: self._netbox = pynetbox.api( self.netbox_uri, token=self.netbox_token, private_key_file=self.netbox_private_key_file) logger.debug('`netbox` context object initialized') else: logger.debug('`netbox` context object accessed') return self._netbox
def common_arguments(self, kwargs, arg, expect, mock): ''' Ensures the api and endpoint instances have ssl_verify set as expected ''' api = pynetbox.api(host, **kwargs) self.assertIs(api.api_kwargs.get(arg, "fail"), expect) for app, endpoint in endpoints.items(): ep = getattr(getattr(api, app), endpoint) self.assertIs(getattr(ep, arg), expect)
def test_threading_duplicates(self, docker_netbox_service, add_sites): api = pynetbox.api( docker_netbox_service["url"], token="0123456789abcdef0123456789abcdef01234567", threading=True, ) test = api.dcim.sites.all(limit=5) test_list = list(test) test_set = set(test_list) assert len(test_list) == len(test_set)
def _get_search_results(self, query: str): nb = pynetbox.api(CONFIG.NETBOX_HOST, token=CONFIG.NETBOX_APIKEY, threading=True) results = [] search_results = nb.dcim.devices.filter(query) for obj in search_results: results.append({ 'name': obj.name, 'type': 'Device', 'module': 'dcim/devices', 'id': obj.id }) search_results = nb.virtualization.virtual_machines.filter(query) for obj in search_results: results.append({ 'name': obj.name, 'type': 'VM', 'module': 'virtualization/virtual-machines', 'id': obj.id }) search_results = nb.ipam.prefixes.filter(query) for obj in search_results: results.append({ 'name': obj.prefix, 'type': 'Prefix', 'module': 'ipam/prefixes', 'id': obj.id }) search_results = nb.ipam.ip_addresses.filter(query) for obj in search_results: results.append({ 'name': obj.address, 'type': 'IP', 'module': 'ipam/ip-addresses', 'id': obj.id }) search_results = nb.dcim.sites.filter(query) for obj in search_results: results.append({ 'name': obj.name, 'type': 'Site', 'module': 'dcim/sites', 'id': obj.id }) search_results = nb.circuits.circuits.filter(query) for obj in search_results: results.append({ 'name': obj.cid, 'type': 'Circuit', 'module': 'circuits/circuits', 'id': obj.id }) return results[:10] if len(results) > 10 else results
def main(): ''' Main entry point for module execution ''' argument_spec = dict( netbox_url=dict(type="str", required=True), netbox_token=dict(type="str", required=True, no_log=True), data=dict(type="dict", required=True), state=dict(required=False, default='present', choices=['present', 'absent']), validate_certs=dict(type="bool", default=True) ) global module module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) # Fail module if pynetbox is not installed if not HAS_PYNETBOX: module.fail_json(msg=missing_required_lib('pynetbox'), exception=PYNETBOX_IMP_ERR) # Fail if device name is not given if not module.params["data"].get("name"): module.fail_json(msg="missing device name") # Assign variables to be used with module app = 'dcim' endpoint = 'devices' url = module.params["netbox_url"] token = module.params["netbox_token"] data = module.params["data"] state = module.params["state"] validate_certs = module.params["validate_certs"] # Attempt to create Netbox API object try: nb = pynetbox.api(url, token=token, ssl_verify=validate_certs) except Exception: module.fail_json(msg="Failed to establish connection to Netbox API") try: nb_app = getattr(nb, app) except AttributeError: module.fail_json(msg="Incorrect application specified: %s" % (app)) nb_endpoint = getattr(nb_app, endpoint) norm_data = normalize_data(data) try: if 'present' in state: result = ensure_device_present(nb, nb_endpoint, norm_data) else: result = ensure_device_absent(nb_endpoint, norm_data) return module.exit_json(**result) except pynetbox.RequestError as e: return module.fail_json(msg=json.loads(e.error)) except ValueError as e: return module.fail_json(msg=str(e))
def load(self): """Initialize pynetbox and load all data from netbox in the local cache.""" inventory_settings = InventorySettings(**config.SETTINGS.inventory.settings) self.netbox = pynetbox.api(url=inventory_settings.address, token=inventory_settings.token) if not inventory_settings.verify_ssl: session = requests.Session() session.verify = False self.netbox.http_session = session self._check_netbox_version() sites = {} device_names = [] results = self.nornir.run(task=query_device_info_from_netbox) for device_name, items in results.items(): if items[0].failed: continue result = items[0].result nb_device = result["device"] site_name = nb_device["site"].get("slug") if site_name not in sites.keys(): site = self.site(name=site_name, remote_id=nb_device["site"].get("id")) sites[site_name] = site self.add(site) else: site = sites[site_name] device = self.device(name=device_name, site_name=site_name, remote_id=nb_device["id"]) if nb_device["primary_ip"]: device.primary_ip = nb_device["primary_ip"].get("address") device = self.apply_model_flag(device, nb_device) self.add(device) # Load Prefix and Vlan per site for site in self.get_all(self.site): self.load_netbox_prefix(site) self.load_netbox_vlan(site) # Load interfaces and IP addresses for each devices devices = self.get_all(self.device) for device in devices: site = sites[device.site_name] device_names.append(device.name) self.load_netbox_device(site=site, device=device) # Load Cabling for site in self.get_all(self.site): self.load_netbox_cable(site=site, device_names=device_names)
def main(): module_args = dict(source=dict(type='path', required=True), api=dict(type='str', required=True), token=dict(type='str', required=True, no_log=True), max_workers=dict(type='int', required=False, default=10)) result = dict(changed=False) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) source = yaml.safe_load(open(module.params['source'])) for device, details in source['devices'].items(): if details is None: source['devices'][device] = {} netbox = pynetbox.api(module.params['api'], token=module.params['token']) sync_args = dict(module=module, netbox=netbox, source=source, before={}, after={}) synchronizers = [ synchronizer(**sync_args) for synchronizer in [ SyncTags, SyncTenants, SyncSites, SyncManufacturers, SyncDeviceTypes, SyncDeviceRoles, SyncDevices, SyncIPs ] ] # Check what needs to be synchronized try: for synchronizer in synchronizers: result['changed'] |= synchronizer.prepare() except AnsibleError as e: result['msg'] = e.message module.fail_json(**result) if module._diff and result['changed']: result['diff'] = [ dict(before_header=table, after_header=table, before=yaml.safe_dump(sync_args["before"][table]), after=yaml.safe_dump(sync_args["after"][table])) for table in sync_args["after"] if sync_args["before"][table] != sync_args["after"][table] ] if module.check_mode or not result['changed']: module.exit_json(**result) # Synchronize for synchronizer in synchronizers: synchronizer.synchronize() for synchronizer in synchronizers[::-1]: synchronizer.cleanup() module.exit_json(**result)
def init(config): """Provide an initialised API endpoint Arguments: config {configParser} -- The enbox config object Returns: pynetbox.api.Api -- an iniitalised pynetbox API instance """ return pynetbox.api(config['NetBox']['Url'], token=config['NetBox']['Token'])
def run(self, terms, variables=None, **kwargs): netbox_api_token = kwargs.get("token") netbox_api_endpoint = kwargs.get("api_endpoint") netbox_private_key_file = kwargs.get("key_file") netbox_api_filter = kwargs.get("api_filter") if not isinstance(terms, list): terms = [terms] netbox = pynetbox.api( netbox_api_endpoint, token=netbox_api_token, private_key_file=netbox_private_key_file, ) results = [] for term in terms: try: endpoint = get_endpoint(netbox, term) except KeyError: raise AnsibleError("Unrecognised term %s. Check documentation" % term) Display().vvvv( u"Netbox lookup for %s to %s using token %s filter %s" % (term, netbox_api_endpoint, netbox_api_token, netbox_api_filter) ) if netbox_api_filter: filter = parse_kv(netbox_api_filter) Display().vvvv("filter is %s" % filter) for res in endpoint.filter(**filter): Display().vvvvv(pformat(dict(res))) key = dict(res)["id"] result = {key: dict(res)} results.extend(self._flatten_hash_to_list(result)) else: for res in endpoint.all(): Display().vvvvv(pformat(dict(res))) key = dict(res)["id"] result = {key: dict(res)} results.extend(self._flatten_hash_to_list(result)) return results
def __init__(self, address, token, tls_verify, tag, cleanup): self.netbox = api(address, token, ssl_verify=tls_verify) self.tag = tag self.cleanup = cleanup self.stats = { 'unchanged': 0, 'created': 0, 'updated': 0, 'deleted': 0, 'errors': 0 }
def __init__(self, url=None, token=None, threading=False, ssl_verify=True): """""" self.url = url self.token = token self.threading = threading self.nb = pynetbox.api(self.url, token=self.token, threading=self.threading) self.nb.http_session = requests.Session() self.nb.http_session.verify = True if ssl_verify else False
def connect_netbox(url=NB_URL, token=API_TOKEN): """ Connect to netbox wo ssl """ # ssl sertificate check disable session = requests.Session() session.verify = False nb = pynetbox.api(url, token=token) nb.http_session = session return nb
def test_create_from_pynetbox(netbox_api_base): api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load( open(f"{ROOT}/../fixtures/netbox_28/ip_address.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) ipaddr = NetboxIPAddressPre29.create_from_pynetbox( diffsync=netbox_api_base, obj=pnb, device_name="HQ-CORE-SW02") assert ipaddr.interface_name == "TenGigabitEthernet1/0/1" assert ipaddr.device_name == "HQ-CORE-SW02"
def nbConnect(server, token): ''' connects to Netbox API requires server and token ''' url = 'http://' + server try: nb = pynetbox.api(url, token=token, ssl_verify=False) except: logger.error("cannot connect to netbox") #sys.exit('cannot connect to netbox') return nb
def main(): ''' Main entry point for module execution ''' argument_spec = dict(netbox_url=dict(type="str", required=True), netbox_token=dict(type="str", required=True, no_log=True), data=dict(type="dict", required=True), state=dict(required=False, default='present', choices=['present', 'absent']), validate_certs=dict(type="bool", default=True)) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) # Fail module if pynetbox is not installed if not HAS_PYNETBOX: module.fail_json(msg=missing_required_lib('pynetbox'), exception=PYNETBOX_IMP_ERR) # Assign variables to be used with module changed = False app = 'ipam' endpoint = 'ip_addresses' url = module.params["netbox_url"] token = module.params["netbox_token"] data = module.params["data"] state = module.params["state"] validate_certs = module.params["validate_certs"] # Attempt to create Netbox API object try: nb = pynetbox.api(url, token=token, ssl_verify=validate_certs) except Exception: module.fail_json(msg="Failed to establish connection to Netbox API") try: nb_app = getattr(nb, app) except AttributeError: module.fail_json(msg="Incorrect application specified: %s" % (app)) nb_endpoint = getattr(nb_app, endpoint) if 'present' in state: response = netbox_create_ip_address(nb, nb_endpoint, data) if response[0].get('created'): changed = True else: response = netbox_delete_ip_address(nb, nb_endpoint, data) if 'success' in response[0]: changed = True module.exit_json(changed=changed, meta=response)
def initialize_netbox_client(): global netbox_client netbox_url = os.environ.get("NETBOX_API_URI") netbox_token = os.environ.get("NETBOX_API_TOKEN") if not netbox_url or not netbox_token: logger.error("Netbox url/token is not set via environment variables") raise SystemExit(-1) netbox_client = pynetbox.api(url=netbox_url, token=netbox_token, ssl_verify=False)
def __init__(self, url: str, token: str, *, dry_run: bool = True): """Create Netbox instance. Arguments: url (str): The Netbox top level URL (with scheme and port if necessary) token (str): A Netbox API token dry_run (bool, optional): set to False to cause writes to Netbox to occur """ self._api = pynetbox.api(url, token=token) self._api.http_session = http_session(".".join( (self.__module__, self.__class__.__name__))) self._dry_run = dry_run
def netbox_init(netbox_url=NETBOX_URL, user_token=NETBOX_AUTOMATE_TOKEN, secure=False) -> pynetbox: protocol = "" if secure: protocol = "https://" else: protocol = "http://" return pynetbox.api( url=f"{protocol}{netbox_url}", token=user_token )
def test_vlan_create_from_pynetbox(netbox_api_base): api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_29}/vlan_101_no_tag.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) item = NetboxVlan.create_from_pynetbox(diffsync=netbox_api_base, obj=pnb, site_name="nyc") assert isinstance(item, NetboxVlan) is True assert item.remote_id == 1 assert item.vid == 101 assert item.associated_devices == []
def main(): ''' Call the stack function to update the interfaces in NetBox''' try: #Connect to NetBox api = pynetbox.api(url=URL, token=TOKEN) #Just shortens the calls later mynb = api.dcim if debug: print("1st Switch name: {}".format(args.switch[0])) stack(args.switch[0], mynb) except: print("There was an error in updating NetBox")
def __init__(self): self.netbox_url = secrets.Secrets.netbox_url self.netbox_ssl_validation = False self.netbox_token = secrets.Secrets.netbox_token self.netbox_connection = pynetbox.api( self.netbox_url, token=self.netbox_token, ssl_verify=self.netbox_ssl_validation) self.netbox_1g_sfp_id = 1100 self.netbox_10g_sfpp_id = 1200 self.netbox_1g_base_t_id = 1000 self.netbox_virtual_id = 0 self.netbox_lag_id = 200 self.netbox_40g_qsfpp_id = 1400
def test_update_clean_tags_no_incoming_tags(netbox_api_base): vlan = NetboxVlanPre29(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_28}/vlan_101_tags_01.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) params = vlan.translate_attrs_for_netbox({"name": "VOICE"}) clean_params = vlan.update_clean_tags(nb_params=params, obj=pnb) assert "tags" not in clean_params
def main(self): display.vvvv("Rabify connecting to %s" % self.api_endpoint) self.nb = pynetbox.api(self.api_endpoint, token=self.api_token, ssl_verify=self.validate_certs) for host in self.inventory.hosts: display.vvv("Rabifying host %s" % host) ip = self._get_host_ip(host) if ip: self.inventory.set_variable(host, "ansible_host", ip.address.split("/")[0]) display.vvv("Rabify updated IP of host %s to %s" % (host, ip))
def __init__(self): """Initializes the Netbox class""" Parser.parse_var(self) self.url = GetSecret("netbox-url").secret_value self.token = GetSecret("netbox-token").secret_value self.nb = pynetbox.api(f"https://{self.url}", self.token, ssl_verify=False) Netbox.create_site(self) Netbox.create_subscriptions(self) Netbox.get_prefixes(self) Netbox.remove_prefixes(self)
def create_netbox_client(config: Config): netbox = pynetbox.api(config.netbox_url, config.netbox_token, threading=config.netbox_threading) session = requests.Session() # disable ssl verify and warnings session.verify = config.netbox_verify_ssl if config.netbox_verify_ssl == False: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) netbox.http_session = session return netbox
def connect(url=None, token=None): ''' Function to connect to netbox :param url: ip address of the netbox host :param token: netbox api token :return: the connection ''' logger.debug('Trying to connect to oats netbox') if not url: url = 'http://10.20.1.10' if not token: token = '20b9f6f9963f94e627198069cc27bc822a84e320' nb = pynetbox.api(url=url, token=token) return nb
def _connect_netbox_api(self, url, token, ssl_verify): try: nb = pynetbox.api(url, token=token, ssl_verify=ssl_verify) try: self.version = float(nb.version) except AttributeError: self.module.fail_json(msg="Must have pynetbox >=4.1.0") except Exception: self.module.fail_json( msg="Failed to establish connection to Netbox API") return nb except Exception: self.module.fail_json( msg="Failed to establish connection to Netbox API")
def __init__(self, address, token, tls_verify, nmap_args, tacacs, tag, unknown): self.netbox = api(address, token=token, ssl_verify=tls_verify) self.nmap_args = nmap_args self.tacacs = tacacs self.tag = tag self.unknown = unknown self.stats = { 'created': 0, 'updated': 0, 'deleted': 0, 'undiscovered': 0, 'duplicated': 0 }
def vmlist_netbox(netbox_inventory, netbox_server, log=False): """Retrieve matching Virtual Machines from NetBox for an inventory""" try: nb = pynetbox.api(netbox_server.url, token=decrypt(netbox_server.api_token)) # Build the VM query from the provided attributes to the inventory instance vm_query = {} if netbox_inventory.site: if log: log.info("Looking up NetBox Sites to Filter.") sites = query_netbox(nb.dcim.sites, log, name=[site for site in netbox_inventory.site]) vm_query["site_id"] = [site.id for site in sites] if netbox_inventory.tenant: if log: log.info("Looking up NetBox Tenants to Filter.") tenants = query_netbox( object=nb.tenancy.tenants, log=log, name=[tenant for tenant in netbox_inventory.tenant], ) vm_query["tenant_id"] = [tenant.id for tenant in tenants] if netbox_inventory.vm_role: if log: log.info("Looking up NetBox Virtual Machine Roles to Filter.") vm_roles = query_netbox( object=nb.dcim.device_roles, log=log, name=[vm_role.role for vm_role in netbox_inventory.vm_role], vm_role=True, ) vm_query["role_id"] = [vm_role.id for vm_role in vm_roles] if log: log.info(f"Looking up NetBox vms for Filter: {vm_query}") vms = query_netbox(object=nb.virtualization.virtual_machines, log=log, **vm_query) return {"status": True, "result": vms} except Exception as e: if log: log.error(f"Lookup failed: {e}") return {"status": False, "result": e}
def check_model(mlist): NB_URL = 'https://*****************' NB_TOKEN = '*****************' lip_nb = pynetbox.api(NB_URL, token=NB_TOKEN) lip_nb.http_session.verify = False result = [] for model in mlist: model_nb = lip_nb.dcim.device_types.get(slug=model.lower()) if model_nb: res_str = ( f'MODEL: {model} Manufacturer {model_nb.manufacturer.slug}') else: res_str = (f'Model {model} not found') result.append(res_str) return result
def _nb_obj(auth_required=False): pynb_kwargs = {} pynb_kwargs['token'] = _config().get('token') if auth_required: pynb_kwargs['private_key_file'] = _config().get('keyfile') return pynetbox.api(_config().get('url'), **pynb_kwargs)