How to use the nornir.core.inventory.Group function in nornir

To help you get started, we’ve selected a few nornir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_add_host(self):
        data = {"test_var": "test_value"}
        defaults = inventory.Defaults(data=data)
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
        h3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
        inv.add_host(
            name="h3",
            groups=["g1"],
            platform="TestPlatform",
            connection_options=h3_connection_options,
        )
        assert "h3" in inv.hosts
        assert "g1" in inv.hosts["h3"].groups
        assert "test_var" in inv.hosts["h3"].defaults.data.keys()
github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_inventory(self):
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups)
        assert "h1" in inv.hosts
        assert "h2" in inv.hosts
        assert "g1" in inv.groups
        assert "g2" in inv.groups
        assert inv.groups["g1"] in inv.hosts["h1"].groups
        assert inv.groups["g1"] in inv.groups["g2"].groups
github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_inventory(self):
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups)
        assert "h1" in inv.hosts
        assert "h2" in inv.hosts
        assert "g1" in inv.groups
        assert "g2" in inv.groups
        assert inv.groups["g1"] in inv.hosts["h1"].groups
        assert inv.groups["g1"] in inv.groups["g2"].groups
github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_add_group(self):
        connection_options = {"username": "test_user", "password": "test_pass"}
        data = {"test_var": "test_value"}
        defaults = inventory.Defaults(data=data, connection_options=connection_options)
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
        g3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
        inv.add_group(
            name="g3", username="test_user", connection_options=g3_connection_options
        )
        assert "g3" in inv.groups
        assert (
            inv.groups["g3"].defaults.connection_options.get("username") == "test_user"
        )
        assert (
            inv.groups["g3"].defaults.connection_options.get("password") == "test_pass"
        )
github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_add_group(self):
        connection_options = {"username": "test_user", "password": "test_pass"}
        data = {"test_var": "test_value"}
        defaults = inventory.Defaults(data=data, connection_options=connection_options)
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
        g3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
        inv.add_group(
            name="g3", username="test_user", connection_options=g3_connection_options
        )
        assert "g3" in inv.groups
        assert (
            inv.groups["g3"].defaults.connection_options.get("username") == "test_user"
        )
        assert (
            inv.groups["g3"].defaults.connection_options.get("password") == "test_pass"
github nornir-automation / nornir / tests / core / test_inventory.py View on Github external
def test_add_host(self):
        data = {"test_var": "test_value"}
        defaults = inventory.Defaults(data=data)
        g1 = inventory.Group(name="g1")
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
        h3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
        inv.add_host(
            name="h3",
            groups=["g1"],
            platform="TestPlatform",
            connection_options=h3_connection_options,
        )
        assert "h3" in inv.hosts
        assert "g1" in inv.hosts["h3"].groups
        assert "test_var" in inv.hosts["h3"].defaults.data.keys()
        assert inv.hosts["h3"].defaults.data.get("test_var") == "test_value"
github nornir-automation / nornir / nornir / core / serializer.py View on Github external
def deserialize(i: Dict[str, Any], *args, **kwargs) -> Inventory:
        serialized = InventorySerializer(**i)
        defaults = ElementData(**serialized.defaults.dict())

        hosts = {}
        for n, h in serialized.hosts.items():
            hosts[n] = Host(name=n, **h.dict())
        groups = {}
        for n, g in serialized.groups.items():
            groups[n] = Group(name=n, **g.dict())

        for h in hosts.values():
            h.defaults = defaults
            h.groups = Groups([groups[n] for n in h.groups])
        for g in groups.values():
            g.defaults = defaults
            g.groups = Groups([groups[n] for n in g.groups])
        return Inventory(hosts, groups, defaults, *args, **kwargs)
github nornir-automation / nornir / nornir / core / inventory.py View on Github external
def __init__(
        self, hosts, groups=None, defaults=None, transform_function=None, nornir=None
    ):
        self._nornir = nornir

        self.defaults = defaults or {}

        self.groups = groups or {}
        for n, g in self.groups.items():
            if isinstance(g, dict):
                g = Group(name=n, nornir=nornir, **g)
            self.groups[n] = g

        for group in self.groups.values():
            group.groups = self._resolve_groups(group.groups)

        self.hosts = {}
        for n, h in hosts.items():
            if isinstance(h, dict):
                h = Host(name=n, nornir=nornir, defaults=self.defaults, **h)

            if transform_function:
                transform_function(h)

            h.groups = self._resolve_groups(h.groups)
            self.hosts[n] = h
github nornir-automation / nornir / nornir / core / deserializer / inventory.py View on Github external
def deserialize_group(cls, **kwargs: Any) -> inventory.Group:
        return inventory.Group(**cls.deserialize(**kwargs))