Exemple #1
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
Exemple #2
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
    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
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
Exemple #5
0
 def __init__(self):
     InventoryPluginRegister.register("OdsDictInventory", OdsDictInventory)
     self.nr = None
Exemple #6
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/")
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)
                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)
Exemple #9
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')").
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(
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)
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"
)
                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)
Exemple #14
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',
        },
    },
)
Exemple #15
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={
Exemple #16
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
# ----------------------------------------------------------------------
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))