-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(anta): NetBox as an inventory source #968
Open
JulioPDX
wants to merge
6
commits into
aristanetworks:main
Choose a base branch
from
JulioPDX:netbox-inv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e2163a9
Initial work to add netbox as an inventory source
JulioPDX 664bb4b
update wording
JulioPDX 9867f03
removing test file
JulioPDX f35a57e
adding pynetbox to install options
JulioPDX 95a589a
adding optional site option
JulioPDX 00e39bd
Merge branch 'main' into netbox-inv
gmuloc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import functools | ||
import importlib | ||
import inspect | ||
import ipaddress | ||
import json | ||
import logging | ||
import pkgutil | ||
|
@@ -214,6 +215,56 @@ def create_inventory_from_ansible(inventory: Path, output: Path, ansible_group: | |
write_inventory_to_file(ansible_hosts, output) | ||
|
||
|
||
def create_inventory_from_netbox(nb_instance: str, output: Path, token: str, platform: str = "Arista EOS", site: str | None = None, verify: bool = False) -> None: | ||
"""Fetch devices from NetBox filtered by a specific platform. | ||
|
||
Parameters | ||
---------- | ||
nb_instance | ||
The NetBox API instance. | ||
output | ||
ANTA inventory file to generate. | ||
token | ||
The token used to authenticate to the NetBox instance. | ||
platform | ||
The platform to filter devices by. | ||
site | ||
The site to filter devices by. | ||
verify | ||
Verify the SSL certification of the NetBox instance. | ||
|
||
""" | ||
session = requests.session() | ||
session.verify = verify | ||
try: | ||
import pynetbox | ||
except ImportError as e: | ||
logging.error(e) | ||
Comment on lines
+239
to
+242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets make a better error message telling what to instal |
||
|
||
try: | ||
# Initialize NetBox API | ||
nb = pynetbox.api(nb_instance, token=token) | ||
|
||
# Platform name to filter | ||
platform = nb.dcim.platforms.get(q=[platform]) | ||
|
||
devices = nb.dcim.devices.filter(platform=platform.slug, site=site) if site else nb.dcim.devices.filter(platform=platform.slug) | ||
|
||
inventory = [] | ||
for device in devices: | ||
host_entry = { | ||
"host": str(ipaddress.ip_interface(device.primary_ip).ip), | ||
"name": device.name, | ||
"tags": [tag.name for tag in device.tags], | ||
} | ||
inventory.append(host_entry) | ||
|
||
write_inventory_to_file(inventory, output) | ||
|
||
except Exception as e: | ||
raise ValueError(e) from e | ||
Comment on lines
+264
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to be more specific on which Exception you can get |
||
|
||
|
||
def explore_package(module_name: str, test_name: str | None = None, *, short: bool = False, count: bool = False) -> int: | ||
"""Parse ANTA test submodules recursively and print AntaTest examples. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1/ Given that you are in the from-netbox command context I dont think you need to add
nb
prefix to the options (except if it makes a lot of sense)2/ the default for security should always be
True
;)3/ I think (and we may not be doing this everywhere) that you don't need
required=False
as it is the default in click. Same fordefault=None