Beispiel #1
0
def config_backup(job_result, data, backup_root_folder):
    """Nornir play to backup configurations."""
    now = datetime.now()
    logger = NornirLogger(__name__, job_result, data.get("debug"))
    global_settings = GoldenConfigSettings.objects.get(id="aaaaaaaa-0000-0000-0000-000000000001")
    verify_global_settings(logger, global_settings, ["backup_path_template", "intended_path_template"])
    nornir_obj = InitNornir(
        runner=NORNIR_SETTINGS.get("runner"),
        logging={"enabled": False},
        inventory={
            "plugin": "nautobot-inventory",
            "options": {
                "credentials_class": NORNIR_SETTINGS.get("credentials"),
                "params": NORNIR_SETTINGS.get("inventory_params"),
                "queryset": get_allowed_os(data),
                "defaults": {"now": now},
            },
        },
    )

    nr_with_processors = nornir_obj.with_processors([ProcessGoldenConfig(logger)])

    nr_with_processors.run(
        task=run_backup,
        name="BACKUP CONFIG",
        logger=logger,
        global_settings=global_settings,
        backup_root_folder=backup_root_folder,
    )

    logger.log_debug("Completed configuration from devices.")
Beispiel #2
0
def config_backup(job_result, data):
    """Nornir play to backup configurations."""
    now = datetime.now()
    logger = NornirLogger(__name__, job_result, data.get("debug"))
    global_settings = GoldenConfigSetting.objects.first()
    verify_global_settings(logger, global_settings, ["backup_path_template"])

    # Build a dictionary, with keys of platform.slug, and the regex line in it for the netutils func.
    remove_regex_dict = {}
    for regex in ConfigRemove.objects.all():
        if not remove_regex_dict.get(regex.platform.slug):
            remove_regex_dict[regex.platform.slug] = []
        remove_regex_dict[regex.platform.slug].append({"regex": regex.regex})

    # Build a dictionary, with keys of platform.slug, and the regex and replace keys for the netutils func.
    replace_regex_dict = {}
    for regex in ConfigReplace.objects.all():
        if not replace_regex_dict.get(regex.platform.slug):
            replace_regex_dict[regex.platform.slug] = []
        replace_regex_dict[regex.platform.slug].append({
            "replace": regex.replace,
            "regex": regex.regex
        })
    try:
        with InitNornir(
                runner=NORNIR_SETTINGS.get("runner"),
                logging={"enabled": False},
                inventory={
                    "plugin": "nautobot-inventory",
                    "options": {
                        "credentials_class":
                        NORNIR_SETTINGS.get("credentials"),
                        "params": NORNIR_SETTINGS.get("inventory_params"),
                        "queryset": get_job_filter(data),
                        "defaults": {
                            "now": now
                        },
                    },
                },
        ) as nornir_obj:

            nr_with_processors = nornir_obj.with_processors(
                [ProcessGoldenConfig(logger)])

            nr_with_processors.run(
                task=run_backup,
                name="BACKUP CONFIG",
                logger=logger,
                global_settings=global_settings,
                remove_regex_dict=remove_regex_dict,
                replace_regex_dict=replace_regex_dict,
            )
            logger.log_debug("Completed configuration from devices.")

    except Exception as err:
        logger.log_failure(None, err)
        raise

    logger.log_debug("Completed configuration backup job for devices.")
Beispiel #3
0
def config_intended(nautobot_job, data, jinja_root_path):
    """
    Nornir play to generate configurations.

    Args:
        nautobot_job (Result): The Nautobot Job instance being run.
        data (dict): Form data from Nautobot Job.
        jinja_root_path (str): The root path to the Jinja2 intended config file.

    Returns:
        None: Intended configuration files are written to filesystem.
    """
    now = datetime.now()
    logger = NornirLogger(__name__, nautobot_job, data.get("debug"))
    global_settings = GoldenConfigSetting.objects.first()
    verify_global_settings(
        logger, global_settings,
        ["jinja_path_template", "intended_path_template", "sot_agg_query"])
    try:
        with InitNornir(
                runner=NORNIR_SETTINGS.get("runner"),
                logging={"enabled": False},
                inventory={
                    "plugin": "nautobot-inventory",
                    "options": {
                        "credentials_class":
                        NORNIR_SETTINGS.get("credentials"),
                        "params": NORNIR_SETTINGS.get("inventory_params"),
                        "queryset": get_job_filter(data),
                        "defaults": {
                            "now": now
                        },
                    },
                },
        ) as nornir_obj:

            nr_with_processors = nornir_obj.with_processors(
                [ProcessGoldenConfig(logger)])

            # Run the Nornir Tasks
            nr_with_processors.run(
                task=run_template,
                name="RENDER CONFIG",
                logger=logger,
                global_settings=global_settings,
                nautobot_job=nautobot_job,
                jinja_root_path=jinja_root_path,
            )

    except Exception as err:
        logger.log_failure(None, err)
        raise
def config_compliance(job_result, data):
    """Nornir play to generate configurations."""
    now = datetime.now()
    rules = get_rules()
    logger = NornirLogger(__name__, job_result, data.get("debug"))
    global_settings = GoldenConfigSetting.objects.first()
    verify_global_settings(logger, global_settings,
                           ["backup_path_template", "intended_path_template"])
    try:
        with InitNornir(
                runner=NORNIR_SETTINGS.get("runner"),
                logging={"enabled": False},
                inventory={
                    "plugin": "nautobot-inventory",
                    "options": {
                        "credentials_class":
                        NORNIR_SETTINGS.get("credentials"),
                        "params": NORNIR_SETTINGS.get("inventory_params"),
                        "queryset": get_job_filter(data),
                        "defaults": {
                            "now": now
                        },
                    },
                },
        ) as nornir_obj:

            nr_with_processors = nornir_obj.with_processors(
                [ProcessGoldenConfig(logger)])

            nr_with_processors.run(
                task=run_compliance,
                name="RENDER COMPLIANCE TASK GROUP",
                logger=logger,
                global_settings=global_settings,
                rules=rules,
            )

    except Exception as err:
        logger.log_failure(None, err)
        raise

    logger.log_debug("Completed compliance job for devices.")
def config_intended(job_result, data, jinja_root_path, intended_root_folder):
    """Nornir play to generate configurations."""
    now = datetime.now()
    logger = NornirLogger(__name__, job_result, data.get("debug"))
    global_settings = GoldenConfigSettings.objects.get(
        id="aaaaaaaa-0000-0000-0000-000000000001")
    verify_global_settings(
        logger, global_settings,
        ["jinja_path_template", "intended_path_template", "sot_agg_query"])
    nornir_obj = InitNornir(
        runner=NORNIR_SETTINGS.get("runner"),
        logging={"enabled": False},
        inventory={
            "plugin": "nautobot-inventory",
            "options": {
                "credentials_class": NORNIR_SETTINGS.get("credentials"),
                "params": NORNIR_SETTINGS.get("inventory_params"),
                "queryset": get_allowed_os(data),
                "defaults": {
                    "now": now
                },
            },
        },
    )

    nr_with_processors = nornir_obj.with_processors(
        [ProcessGoldenConfig(logger)])

    # Run the Nornir Tasks
    nr_with_processors.run(
        task=run_template,
        name="RENDER CONFIG",
        logger=logger,
        global_settings=global_settings,
        job_result=job_result,
        jinja_root_path=jinja_root_path,
        intended_root_folder=intended_root_folder,
    )