Created
August 13, 2025 11:12
-
-
Save johanek/76b06ea7c734f8285eeb09765cb0db0a to your computer and use it in GitHub Desktop.
nautobot config sync client
This file contains hidden or 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
| import pynautobot | |
| class NautobotClient: | |
| def __init__(self, base_url, token): | |
| self.base_url = base_url | |
| self.token = token | |
| self.nb = pynautobot.api(self.base_url, token=self.token) | |
| def get_devices(self): | |
| query = """ | |
| { | |
| devices(role: "CPE") { | |
| name | |
| } | |
| } | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return [r['name'] for r in result.json['data']['devices']] | |
| def get_config_compliances(self): | |
| query = """ | |
| { | |
| config_compliances(device_status: ["Active", "Provisioning"]) { | |
| id | |
| device { | |
| name | |
| role { | |
| name | |
| } | |
| location { | |
| name | |
| parent { | |
| name | |
| } | |
| } | |
| platform { | |
| name | |
| } | |
| } | |
| compliance | |
| actual | |
| intended | |
| missing | |
| extra | |
| rule { | |
| feature { | |
| name | |
| } | |
| } | |
| } | |
| } | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['config_compliances'] | |
| def get_devices(self): | |
| query = """ | |
| { | |
| devices(status: ["Active", "Provisioning"]) { | |
| name | |
| role { | |
| name | |
| } | |
| location { | |
| name | |
| parent { | |
| name | |
| } | |
| } | |
| platform { | |
| name | |
| } | |
| } | |
| } | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['devices'] | |
| def get_features(self): | |
| query = """ | |
| { | |
| compliance_features { | |
| name | |
| } | |
| } | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['compliance_features'] | |
| def get_config_compliances_device(self, device_name): | |
| query = f""" | |
| {{ | |
| config_compliances(device: "{device_name}") {{ | |
| id | |
| device {{ | |
| name | |
| role {{ | |
| name | |
| }} | |
| location {{ | |
| name | |
| parent {{ | |
| name | |
| }} | |
| }} | |
| platform {{ | |
| name | |
| }} | |
| }} | |
| compliance | |
| actual | |
| intended | |
| missing | |
| extra | |
| rule {{ | |
| feature {{ | |
| name | |
| }} | |
| }} | |
| }} | |
| }} | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['config_compliances'] | |
| def get_config_compliances_device(self, device_name): | |
| query = f""" | |
| {{ | |
| config_compliances(device: "{device_name}") {{ | |
| id | |
| device {{ | |
| name | |
| role {{ | |
| name | |
| }} | |
| location {{ | |
| name | |
| parent {{ | |
| name | |
| }} | |
| }} | |
| platform {{ | |
| name | |
| }} | |
| }} | |
| compliance | |
| actual | |
| intended | |
| missing | |
| extra | |
| rule {{ | |
| feature {{ | |
| name | |
| }} | |
| }} | |
| }} | |
| }} | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['config_compliances'] | |
| def get_config_compliances_device_feature(self, device_name, feature_name): | |
| query = f""" | |
| {{ | |
| config_compliances(device: "{device_name}", feature: "{feature_name}") {{ | |
| id | |
| device {{ | |
| name | |
| role {{ | |
| name | |
| }} | |
| location {{ | |
| name | |
| parent {{ | |
| name | |
| }} | |
| }} | |
| platform {{ | |
| name | |
| }} | |
| }} | |
| compliance | |
| actual | |
| intended | |
| missing | |
| extra | |
| rule {{ | |
| feature {{ | |
| name | |
| }} | |
| }} | |
| }} | |
| }} | |
| """ | |
| result = self.nb.graphql.query(query) | |
| return result.json['data']['config_compliances'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment