Esempio n. 1
0
 def test_registered_inventory(self):
     InventoryPluginRegister.deregister_all()
     InventoryPluginRegister.auto_register()
     assert InventoryPluginRegister.available == {
         "SimpleInventory": SimpleInventory,
         "YAMLInventory": YAMLInventory,
     }
Esempio n. 2
0
def cnaas_init() -> Nornir:
    InventoryPluginRegister.register("CnaasInventory", CnaasInventory)
    nr = InitNornir(runner={
        "plugin": "threaded",
        "options": {
            "num_workers": 50
        }
    },
                    inventory={"plugin": "CnaasInventory"},
                    logging={
                        "log_file":
                        "/tmp/nornir-pid{}.log".format(os.getpid()),
                        "level": "DEBUG"
                    })
    return nr
def load_inventory(config: Config, ) -> Inventory:
    InventoryPluginRegister.auto_register()
    inventory_plugin = InventoryPluginRegister.get_plugin(
        config.inventory.plugin)
    inv = inventory_plugin(**config.inventory.options).load()

    if config.inventory.transform_function:
        TransformFunctionRegister.auto_register()
        transform_function = TransformFunctionRegister.get_plugin(
            config.inventory.transform_function)
        for h in inv.hosts.values():
            transform_function(
                h, **(config.inventory.transform_function_options or {}))

    return inv
Esempio n. 4
0
def default_nr_init(pytester):
    """Create initial Nornir files and expose the location
    as nornir_config_file fixture."""
    hosts_path = pytester.path / f"hosts{YAML_EXTENSION}"
    config = f"""inventory:
                          plugin: SimpleInventory
                          options:
                              host_file: {hosts_path}"""
    arguments = {
        "nr-config": config,
        "hosts": """
            R1:
              hostname: 1.1.1.1
            R2:
              hostname: 2.2.2.2
            R3:
              hostname: 3.3.3.3
            L1:
              hostname: 11.11.11.11
            L2:
              hostname: 22.22.22.22
            S1:
              hostname: 111.111.111.111
            S2:
              hostname: 222.222.222.222""",
    }
    pytester.makefile(YAML_EXTENSION, **arguments)

    # We need to have the test tmpdir in sys.path, so NUTS can import the test
    # modules (e.g. basic_task.py).
    pytester.syspathinsert()

    yield

    # Cleanup Nornir's PluginRegisters.
    # This is necessary as InitNornir is initiated for every test case, but the
    # PluginRegisters are (somehow) shared. This results in a
    # PluginAlreadyRegistered Exception as the plugins are registered multiple
    # times.
    ConnectionPluginRegister.deregister_all()
    InventoryPluginRegister.deregister_all()
    RunnersPluginRegister.deregister_all()
Esempio n. 5
0
def main():

    InventoryPluginRegister.register("ExcelInventory", ExcelInventory)
    nr = InitNornir(runner={
        "plugin": "threaded",
        "options": {
            "num_workers": 100,
        },
    },
                    inventory={
                        "plugin": "ExcelInventory",
                        "options": {
                            "excel_filename": "SwitchList.xlsx"
                        }
                    })

    nr.run(name="test", task=main_task)

    # import ipdb; ipdb.set_trace()

    return
Esempio n. 6
0
    def build_inventory(self, limit=None):
        """Build the inventory for the Network Importer in Nornir format."""
        # pylint: disable=import-outside-toplevel
        # Load build-in Inventories as needed
        if config.SETTINGS.inventory.inventory_class == "NetBoxAPIInventory":
            from network_importer.adapters.netbox_api.inventory import NetBoxAPIInventory

            InventoryPluginRegister.register("NetBoxAPIInventory",
                                             NetBoxAPIInventory)
        elif config.SETTINGS.inventory.inventory_class == "NautobotAPIInventory":
            from network_importer.adapters.nautobot_api.inventory import NautobotAPIInventory

            InventoryPluginRegister.register("NautobotAPIInventory",
                                             NautobotAPIInventory)

        self.nornir = InitNornir(
            runner={
                "plugin": "threaded",
                "options": {
                    "num_workers": config.SETTINGS.main.nbr_workers
                }
            },
            logging={"enabled": False},
            inventory={
                "plugin": config.SETTINGS.inventory.inventory_class,
                "options": {
                    "username": config.SETTINGS.network.login,
                    "password": config.SETTINGS.network.password,
                    "enable": config.SETTINGS.network.enable,
                    "supported_platforms":
                    config.SETTINGS.inventory.supported_platforms,
                    "netmiko_extras": config.SETTINGS.network.netmiko_extras,
                    "napalm_extras": config.SETTINGS.network.napalm_extras,
                    "limit": limit,
                    "settings": config.SETTINGS.inventory.settings,
                },
            },
        )

        return True
Esempio n. 7
0
    ops_srvs = ops.Servers(name=tname, dbg=logging.INFO)
    ops_srvs.check_hosts_online()


def gen_heat_os(name_config_yaml) -> None:
    logger.info("================ Run gen-heat-openstack =================")
    os.chdir(cur_dir.parent)
    ht = ym.DevicesInfo(name_config_yaml, dbg=logging.INFO)
    ym.YamlToHeat(ht, dbg=logging.INFO)
    os.chdir(cur_dir)


if __name__ == "__main__":
    TEMPLATES_DIR = "/Users/alex/Dropbox/automation/net-labsv2/playbooks/templates"
    logger = AkarLogging(logging.INFO, "Openstack Labs").get_color_logger()
    InventoryPluginRegister.register("LabInventory", MyLabInventory)

    args = cmdArgsParser()
    stack_name = args.name
    stack_wan = args.wan or "wan0"
    cur_dir = Path.cwd()
    cur_name_dir = str(cur_dir.name)
    cur_name_base = (re.match(r'^(.*)v\d+$', cur_name_dir)).group(1)
    stack_template = f'st_{cur_name_base}.yaml'
    # print(f'{cur_name_base} = {stack_template}')
    if not args.skip:
        gen_heat_os(f'{cur_name_base}.yaml')
        create_stack(stack_name, stack_template)
        check_stack_online(stack_name)
    nr = InitNornir(
        runner={
Esempio n. 8
0
        logging={"enabled": False},
        runner={"plugin": "serial"},
        inventory={
            "plugin": "DictInventory",
            "options": {
                "hosts": opts["hosts"],
                "groups": opts.get("groups", {}),
                "defaults": opts.get("defaults", {}),
            },
        },
    )

    return nr


InventoryPluginRegister.register("DictInventory", DictInventory)

nr = init(lab_inventory_dict)


def clean_up_folder():
    # remove previous files and folder
    if os.path.exists("./tofile_outputs/"):
        for filen in os.listdir("./tofile_outputs/"):
            os.remove("./tofile_outputs/" + filen)
        os.rmdir("./tofile_outputs/")


# ----------------------------------------------------------------------
# tests that need Nornir
# ----------------------------------------------------------------------
Esempio n. 9
0
#!/usr/bin/env python3

from nornir import InitNornir
from nornir.core.inventory import Inventory
from nornir.core.plugins.inventory import InventoryPluginRegister
from akarlibs.nornir.inventory.ansible import AnsibleInventory
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.functions import print_result
from nornir_scrapli.tasks import send_command
import json
import pprint
import ipdb
import logging

InventoryPluginRegister.register("LabInventory", AnsibleInventory)
# InventoryPluginRegister.register("AnsibleInventory", AnsibleInventory)

nr = InitNornir(
    runner={
        "plugin": "threaded",
        "options": {
            "num_workers": 100,
        },
    },
    inventory={
        'plugin': 'LabInventory',
        'options': {
            'hostsfile': 'hosts.yaml',
        },
    },
)
from nornir.core.plugins.inventory import InventoryPluginRegister
from nornir.core.task import Result, Task

from nornir_nautobot.plugins.tasks.dispatcher import dispatcher
from nornir_nautobot.utils.logger import NornirLogger
from nornir_nautobot.exceptions import NornirNautobotException

from nautobot_plugin_nornir.plugins.inventory.nautobot_orm import NautobotORMInventory
from nautobot_plugin_nornir.constants import NORNIR_SETTINGS

from nautobot_golden_config.models import GoldenConfigSettings, GoldenConfiguration
from nautobot_golden_config.utilities.helper import get_allowed_os, verify_global_settings, check_jinja_template
from nautobot_golden_config.utilities.graphql import graph_ql_query
from .processor import ProcessGoldenConfig

InventoryPluginRegister.register("nautobot-inventory", NautobotORMInventory)
LOGGER = logging.getLogger(__name__)


def run_template(  # pylint: disable=too-many-arguments
        task: Task, logger, global_settings, job_result, jinja_root_path,
        intended_root_folder) -> Result:
    """Render Jinja Template.

    Only one template is supported, so the expectation is that that template includes all other templates.

    Args:
        task (Task): Nornir task individual object

    Returns:
        result (Result): Result from Nornir task
Esempio n. 11
0
 def __init__(self):
     InventoryPluginRegister.register("OdsDictInventory", OdsDictInventory)
     self.nr = None
import pprint
import os
import sys
import pprint
from nornir.core.plugins.inventory import InventoryPluginRegister
sys.path.insert(1, '../')
from inventory_script.helpers import adapt_user_password
from nornir import InitNornir
from nornir_utils.plugins.functions import print_result
from Nornir_Configuration.vlanConfig import configure_vlan
from Nornir_Configuration.Backup_configuration import backup_configuration
from nornir.core.filter import F

InventoryPluginRegister.register("user_password", adapt_user_password)
config_files = os.path.join('../config.yaml')
nr = InitNornir(config_files)


def main():
    client_access_devices = nr.filter(F(device_role__name="Access"))
    client_distribution_devices = nr.filter(
        F(device_role__name="Distribution"))
    client_core_devices = nr.filter(F(device_role__name="Core"))
    access_task = client_access_devices.run(task=backup_configuration)
    print_result(access_task)
    distribution_task = client_distribution_devices.run(
        task=backup_configuration)
    print_result(distribution_task)
    core_task = client_core_devices.run(task=backup_configuration)
    print_result(core_task)
Esempio n. 13
0
    def __init__(self, *args, **kwargs):
        pass

    def load(self):
        return Inventory(
            hosts=Hosts({
                "h1": Host("h1"),
                "h2": Host("h2"),
                "h3": Host("h3")
            }),
            groups=Groups({"g1": Group("g1")}),
            defaults=Defaults(),
        )


InventoryPluginRegister.register("inventory-test", InventoryTest)
TransformFunctionRegister.register("transform_func", transform_func)
TransformFunctionRegister.register("transform_func_with_options",
                                   transform_func_with_options)


class Test(object):
    def test_InitNornir_bare(self):
        os.chdir("tests/inventory_data/")
        nr = InitNornir()
        os.chdir("../../")
        assert len(nr.inventory.hosts)
        assert len(nr.inventory.groups)

    def test_InitNornir_defaults(self):
        os.chdir("tests/inventory_data/")
Esempio n. 14
0
                host.connection_options = {
                    "napalm":
                    ConnectionOptions(
                        platform=platforms_mapping[dev.platform.slug])
                }

            if dev.platform:
                host.platform = dev.platform.slug
            else:
                host.platform = None

            host.groups = ParentGroups([self.global_group])

            if dev.site.slug not in groups.keys():
                groups[dev.site.slug] = {}

            if dev.device_role.slug not in groups.keys():
                groups[dev.device_role.slug] = {}

            if host.hostname and host.platform:
                host.is_reachable = True

            # Assign temporary dict to outer dict

            hosts[dev_name] = host

        return Inventory(hosts=hosts, groups=groups, defaults=defaults)


InventoryPluginRegister.register("NautobotAPIInventory", NautobotAPIInventory)
from nornir import InitNornir

# Custom inventory - json format
from nornir.core.plugins.inventory import InventoryPluginRegister
from simpleJson import SimpleInventoryJson
InventoryPluginRegister.register("simpleJson", SimpleInventoryJson)


nr = InitNornir(config_file='config.yaml',
                inventory={
                            "plugin": "simpleJson",
                            "options": {
                                "host_file" : "inventory/hosts.json",
                                "group_file": "inventory/groups.json",
                                "defaults_file": "inventory/defaults.json"
                            },
                        })
print(len(nr.inventory.hosts))

Esempio n. 16
0
                host.connection_options = {
                    "napalm":
                    ConnectionOptions(
                        platform=platforms_mapping[dev.platform.slug])
                }

            if dev.platform:
                host.platform = dev.platform.slug
            else:
                host.platform = None

            host.groups = ParentGroups([self.global_group])

            if dev.site.slug not in groups.keys():
                groups[dev.site.slug] = {}

            if dev.device_role.slug not in groups.keys():
                groups[dev.device_role.slug] = {}

            if host.hostname and host.platform:
                host.is_reachable = True

            # Assign temporary dict to outer dict

            hosts[dev_name] = host

        return Inventory(hosts=hosts, groups=groups, defaults=defaults)


InventoryPluginRegister.register("NetBoxAPIInventory", NetBoxAPIInventory)
Esempio n. 17
0
from nornir import InitNornir
from nornir.core import configuration
from nornir.plugins.runners import ThreadedRunner, SerialRunner
from nornir.core.plugins.inventory import InventoryPluginRegister
import logging

from nornir_utils.plugins.functions import print_result
from nornir_utils.plugins.tasks.files import write_file
from nornir_napalm.plugins.tasks import *
from nornir_netmiko.tasks import *
from nornir_jinja2.plugins.tasks import *
from nornir_netbox.plugins.inventory import NetBoxInventory2
from mytasks import *
from mytasks.batfishTasks import *

InventoryPluginRegister.register("SerialRunner", SerialRunner)
InventoryPluginRegister.register("ThreadedRunner", ThreadedRunner)
InventoryPluginRegister.register("NetBoxInventory2", NetBoxInventory2)

nr = InitNornir(config_file='config.yaml', dry_run=False)

nr = nr.filter(name='localhost')


def batfish_tasks(task):
    task.run(
        task=BF_session,
        host="localhost",
    )

    task.run(
Esempio n. 18
0
##Import and register custom runner
from nornir.core.plugins.runners import RunnersPluginRegister
from custom_runners import (
    runner_as_completed,
    runner_as_completed_tqdm,
    runner_as_completed_rich,
)

RunnersPluginRegister.register("my_runner", runner_as_completed)
# RunnersPluginRegister.register("my_runner", runner_as_completed_tqdm)
# RunnersPluginRegister.register("my_runner", runner_as_completed)
##Import and register custom inventory
from lnetd_nornir_inventory import LnetDInventory
from nornir.core.plugins.inventory import InventoryPluginRegister

InventoryPluginRegister.register("LnetDInventory", LnetDInventory)

##Import plugins
from nornir_napalm.plugins.tasks import napalm_get
from nornir_netmiko import netmiko_send_command
from nornir_utils.plugins.functions import print_title


def write_sql(df):
    try:
        disk_engine = create_engine("sqlite:////opt/lnetd/web_app/database.db")
        df.to_sql("Links_latency", disk_engine, if_exists="replace")
        """
        add_to_table = disk_engine.execute(text("insert into Links_latency_time select null,*,DATETIME('now') from Links_latency").
                                           execution_options(autocommit=True))
        delete_old_values = disk_engine.execute(text("DELETE FROM Links_latency_time WHERE timestamp <= date('now','-12 day')").
import os
import logging
from tqdm import tqdm
from nornir import InitNornir
from nornir_utils.plugins.functions import print_title, print_result
from nornir_utils.plugins.tasks.data import echo_data
from nornir_netmiko import netmiko_send_config, netmiko_save_config, netmiko_send_command
from nornir_jinja2.plugins.tasks import template_file
from nornir.core.exceptions import NornirSubTaskError, ConnectionException
from nornir.core.inventory import ConnectionOptions
from nornir.core.plugins.inventory import TransformFunctionRegister
from nornir.core.plugins.inventory import InventoryPluginRegister
from plugins.inventory import InventoryFromDict
import multiprocessing

InventoryPluginRegister.register('InventoryFromDict', InventoryFromDict)

inventory = {'SW1': {'hostname': '192.168.1.201', 'platform': 'ios'}}

os.mkdir('logs') if not os.path.isdir('logs') else None
os.mkdir('logs/devices') if not os.path.isdir('logs/devices') else None


def host_credentials(host, username, password):
    host.username = username
    host.password = password
    host.connection_options['netmiko'] = ConnectionOptions(
        extras={'secret': password})


TransformFunctionRegister.register('HostCredentials', host_credentials)
Esempio n. 20
0
from nornir.core import configuration
from nornir.plugins.runners import ThreadedRunner, SerialRunner
from nornir.core.plugins.inventory import InventoryPluginRegister
import logging
from nornir_utils.plugins.functions import print_result
from nornir_utils.plugins.tasks.files import write_file
from nornir_napalm.plugins.tasks import *
from nornir_netmiko.tasks import *
from nornir_jinja2.plugins.tasks import *
from nornir.core.filter import F
from mytasks import *

import _thread
import time

InventoryPluginRegister.register("SerialRunner", SerialRunner)
InventoryPluginRegister.register("ThreadedRunner", ThreadedRunner)

nr = InitNornir(config_file='config.yaml', dry_run=False)
nr = nr.filter(F(groups__contains="IOS"))
# Module constants
CAT_FACTS_URL = 'https://catfact.ninja/fact'
# Initialize the environment
# Create the web application instance
flask_app = Flask(__name__)
# Create the Webex Teams API connection object
api = WebexTeamsAPI(
    access_token=
    "OGMzMjEyYWItNzU4NC00ODQ0LTljYzYtMGVlYzUxYzgzMTNmYWE4NzA5MDEtNzkz_PF84_1eb65fdf-9643-417f-9974-ad72cae0e10f"
)