def load(self) -> Inventory: yml = ruamel.yaml.YAML(typ="safe") if self.defaults_file.exists(): with open(self.defaults_file, "r") as f: defaults_dict = yml.load(f) or {} defaults = _get_defaults(defaults_dict) else: defaults = Defaults() hosts = Hosts() with open(self.host_file, "r") as f: hosts_dict = yml.load(f) for n, h in hosts_dict.items(): hosts[n] = _get_inventory_element(Host, h, n, defaults) groups = Groups() if self.group_file.exists(): with open(self.group_file, "r") as f: groups_dict = yml.load(f) or {} for n, g in groups_dict.items(): groups[n] = _get_inventory_element(Group, g, n, defaults) for h in hosts.values(): h.groups = ParentGroups([groups[g] for g in h.groups]) for g in groups.values(): g.groups = ParentGroups([groups[g] for g in g.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)
def load(self) -> Inventory: if self.defaults_dict: defaults = _get_defaults(self.defaults_dict) else: defaults = Defaults() hosts = Hosts() hosts_dict = self.host_dict for n, h in hosts_dict.items(): hosts[n] = _get_inventory_element(Host, h, n, defaults) groups = Groups() if self.group_dict: groups_dict = self.group_dict for n, g in groups_dict.items(): groups[n] = _get_inventory_element(Group, g, n, defaults) for h in hosts.values(): h.groups = ParentGroups([groups[g] for g in h.groups]) for g in groups.values(): g.groups = ParentGroups([groups[g] for g in g.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)
def load(self) -> Inventory: """Standard Nornir 3 load method boilerplate.""" if not self.credentials_params: self.credentials_params = {} # Initialize QuerySet if not self.queryset: self.queryset = Device.objects.all() if self.filters: self.queryset = self.queryset.filter(**self.filters) if not self.params: self.params = {} self.queryset = self.queryset.select_related( "device_role", "device_type", "device_type__manufacturer", "site", "platform", "tenant", ) # Initialize Hosts and Groups vars hosts = Hosts() defaults = Defaults(data=self.defaults) groups = Groups() if self.credentials_params: cred = self.cred_class(params=self.credentials_params) else: cred = self.cred_class() # Create all hosts for device in self.queryset: host = self.create_host(device=device, cred=cred, params=self.params) hosts[device.name] = _set_host(data=host["data"], name=host["name"], groups=host["groups"], host=host, defaults=defaults) # Initialize all groups if they don't already exist for group in hosts[device.name].groups: if group not in groups.keys(): groups[group] = Group(name=group, defaults=defaults) for _host in hosts.values(): _host.groups = ParentGroups( [groups[_group] for _group in _host.groups]) for _group in groups.values(): _group.groups = ParentGroups( [groups[_group] for _group in _group.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)
def load(self) -> Inventory: defaults = _get_defaults(self.defaults_dict) hosts = Hosts() hosts_dict = _get_devices_excel(self._excel_file) for n, h in hosts_dict.items(): hosts[n] = _get_inventory_element(Host, h, n, defaults) groups = Groups() for n, g in self.groups_dict.items(): groups[n] = _get_inventory_element(Group, g, n, defaults) for h in hosts.values(): h.groups = ParentGroups([groups[g] for g in h.groups]) for g in groups.values(): g.groups = ParentGroups([groups[g] for g in g.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)
def load(self) -> Inventory: if self.defaults_file.exists(): with open(self.defaults_file, "r") as f: try: defaults_dict = json.load(f) except JSONDecodeError: defaults_dict = {} defaults = _get_defaults(defaults_dict) else: defaults = Defaults() hosts = Hosts() with open(self.host_file, "r") as f: hosts_dict = json.load(f) for n, h in hosts_dict.items(): hosts[n] = _get_inventory_element(Host, h, n, defaults) groups = Groups() if self.group_file.exists(): with open(self.group_file, "r") as f: try: groups_dict = json.load(f) except JSONDecodeError: groups_dict = {} for n, g in groups_dict.items(): groups[n] = _get_inventory_element(Group, g, n, defaults) for h in hosts.values(): h.groups = ParentGroups([groups[g] for g in h.groups]) for g in groups.values(): g.groups = ParentGroups([groups[g] for g in g.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)
def inventory_from_yaml(): dir_path = os.path.dirname(os.path.realpath(__file__)) yml = ruamel.yaml.YAML(typ="safe") def get_connection_options(data): cp = {} for cn, c in data.items(): cp[cn] = ConnectionOptions( hostname=c.get("hostname"), port=c.get("port"), username=c.get("username"), password=c.get("password"), platform=c.get("platform"), extras=c.get("extras"), ) return cp def get_defaults(): defaults_file = f"{dir_path}/inventory_data/defaults.yaml" with open(defaults_file, "r") as f: defaults_dict = yml.load(f) defaults = Defaults( hostname=defaults_dict.get("hostname"), port=defaults_dict.get("port"), username=defaults_dict.get("username"), password=defaults_dict.get("password"), platform=defaults_dict.get("platform"), data=defaults_dict.get("data"), connection_options=get_connection_options( defaults_dict.get("connection_options", {})), ) return defaults def get_inventory_element(typ, data, name, defaults): return typ( name=name, hostname=data.get("hostname"), port=data.get("port"), username=data.get("username"), password=data.get("password"), platform=data.get("platform"), data=data.get("data"), groups=data.get( "groups" ), # this is a hack, we will convert it later to the correct type defaults=defaults, connection_options=get_connection_options( data.get("connection_options", {})), ) host_file = f"{dir_path}/inventory_data/hosts.yaml" group_file = f"{dir_path}/inventory_data/groups.yaml" defaults = get_defaults() hosts = Hosts() with open(host_file, "r") as f: hosts_dict = yml.load(f) for n, h in hosts_dict.items(): hosts[n] = get_inventory_element(Host, h, n, defaults) groups = Groups() with open(group_file, "r") as f: groups_dict = yml.load(f) for n, g in groups_dict.items(): groups[n] = get_inventory_element(Group, g, n, defaults) for h in hosts.values(): h.groups = ParentGroups([groups[g] for g in h.groups]) for g in groups.values(): g.groups = ParentGroups([groups[g] for g in g.groups]) return Inventory(hosts=hosts, groups=groups, defaults=defaults)