コード例 #1
0
def netconf_unlock(
    task: Task,
    target: str,
) -> Result:
    """
    Unlock the device with scrapli_netconf

    Args:
        task: nornir task object
        target: configuration source to target; running|startup|candidate

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.unlock(target=target)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=True,
    )
    return result
コード例 #2
0
def netconf_edit_config(
    task: Task,
    config: str,
    target: str = "running",
) -> Result:
    """
    Edit config from the device with scrapli_netconf

    Args:
        task: nornir task object
        config: configuration to send to device
        target: configuration source to target; running|startup|candidate

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get_config operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.edit_config(config=config, target=target)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=True,
    )
    return result
コード例 #3
0
def netconf_discard(task: Task, ) -> Result:
    """
    Discard the device config with scrapli_netconf

    Args:
        task: nornir task object

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.discard()

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=True,
    )
    return result
コード例 #4
0
def netconf_get(
    task: Task,
    filter_: str,
    filter_type: str = "subtree",
) -> Result:
    """
    Get from the device with scrapli_netconf

    Args:
        task: nornir task object
        filter_: string filter to apply to the get
        filter_type: type of filter; subtree|xpath

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.get(filter_=filter_,
                                        filter_type=filter_type)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result
コード例 #5
0
def netconf_rpc(
    task: Task,
    filter_: str,
) -> Result:
    """
    Send a "bare" rcp to the device with scrapli_netconf

    Args:
        task: nornir task object
        filter_: filter/rpc to execute

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            rpc operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf", task.nornir.config)
    scrapli_response = scrapli_conn.rpc(filter_=filter_)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=True,
    )
    return result
コード例 #6
0
def netconf_validate(
    task: Task,
    source: str,
) -> Result:
    """
    Send a "validate" rcp to the device with scrapli_netconf

    Args:
        task: nornir task object
        source: configuration source to validate; typically one of running|startup|candidate

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.validate(source=source)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result
コード例 #7
0
def netconf_edit_config(
    task: Task,
    config: str,
    dry_run: Optional[bool] = None,
    diff: bool = False,
    target: str = "running",
) -> Result:
    """
    Edit config from the device with scrapli_netconf

    Args:
        task: nornir task object
        config: configuration to send to device
        dry_run: if True config will be pushed and then discarded; will discard anything already
            pushed that has *not* been committed already, so be careful! :D; also note that this
            will only work if there is a candidate datastore -- meaning that, for example, with
            IOSXE with a target of "running" there is no way to discard the configuration as it will
            already have been written to the running datastore
        diff: capture/set diff of target datastore xml text of before/after edit config operation
        target: configuration source to target; running|startup|candidate

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get_config operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf", task.nornir.config)

    if diff:
        original_config = scrapli_conn.get_config(source=target)

    scrapli_response = scrapli_conn.edit_config(config=config, target=target)

    if diff:
        edited_config = scrapli_conn.get_config(source=target)
        diff_result = diff_xml_text(original_config.result, edited_config.result)
    else:
        diff_result = ""

    _task_dry_run = dry_run if dry_run is not None else task.global_dry_run

    if _task_dry_run:
        scrapli_conn.discard()
        changed = False
    else:
        changed = True

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=changed,
        diff=diff_result,
    )
    return result
コード例 #8
0
def send_commands(
    task: Task,
    commands: List[str],
    strip_prompt: bool = True,
    failed_when_contains: Optional[Union[str, List[str]]] = None,
    stop_on_failed: bool = False,
    timeout_ops: Optional[float] = None,
) -> Result:
    """
    Send a list of commands to device using scrapli

    Args:
        task: nornir task object
        commands: list of strings to send to device in privilege exec mode
        strip_prompt: True/False strip prompt from returned output
        failed_when_contains: string or list of strings indicating failure if found in response
        stop_on_failed: True/False stop executing commands if a command fails, returns results as of
            current execution
        timeout_ops: timeout ops value for this operation; only sets the timeout_ops value for
            the duration of the operation, value is reset to initial value after operation is
            completed. Note that this is the timeout value PER COMMAND sent, not for the total
            of the commands being sent!

    Returns:
        Result: nornir result object with Result.result value set to returned scrapli Response
            object

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli", task.nornir.config)
    scrapli_response = scrapli_conn.send_commands(
        commands=commands,
        strip_prompt=strip_prompt,
        failed_when_contains=failed_when_contains,
        stop_on_failed=stop_on_failed,
        timeout_ops=timeout_ops,
    )

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result
コード例 #9
0
def send_command(
    task: Task,
    command: str,
    strip_prompt: bool = True,
    failed_when_contains: Optional[Union[str, List[str]]] = None,
    timeout_ops: Optional[float] = None,
) -> Result:
    """
    Send a single command to device using scrapli

    Args:
        task: nornir task object
        command: string to send to device in privilege exec mode
        strip_prompt: True/False strip prompt from returned output
        failed_when_contains: string or list of strings indicating failure if found in response
        timeout_ops: timeout ops value for this operation; only sets the timeout_ops value for
            the duration of the operation, value is reset to initial value after operation is
            completed

    Returns:
        Result: scrapli nornir result object; almost identical to a "normal" nornir result object,
            but contains an additional attribute "scrapli_response" that contains the original
            response from scrapli

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli", task.nornir.config)
    scrapli_response = scrapli_conn.send_command(
        command=command,
        strip_prompt=strip_prompt,
        failed_when_contains=failed_when_contains,
        timeout_ops=timeout_ops,
    )

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result
コード例 #10
0
def send_commands_from_file(
    task: Task,
    file: str,
    strip_prompt: bool = True,
    failed_when_contains: Optional[Union[str, List[str]]] = None,
    stop_on_failed: bool = False,
) -> Result:
    """
    Send a list of commands from a file to device using scrapli

    Args:
        task: nornir task object
        file: string path to file
        strip_prompt: True/False strip prompt from returned output
        failed_when_contains: string or list of strings indicating failure if found in response
        stop_on_failed: True/False stop executing commands if a command fails, returns results as of
            current execution

    Returns:
        Result: nornir result object with Result.result value set to returned scrapli Response
            object

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli", task.nornir.config)
    scrapli_response = scrapli_conn.send_commands_from_file(
        file=file,
        strip_prompt=strip_prompt,
        failed_when_contains=failed_when_contains,
        stop_on_failed=stop_on_failed,
    )

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result
コード例 #11
0
def netconf_get_config(
    task: Task,
    source: str = "running",
    filters: Optional[Union[str, List[str]]] = None,
    filter_type: str = "subtree",
) -> Result:
    """
    Get config from the device with scrapli_netconf

    Args:
        task: nornir task object
        source: configuration source to get; typically one of running|startup|candidate
        filters: string or list of strings of filters to apply to configuration
        filter_type: type of filter; subtree|xpath

    Returns:
        Result: nornir result object with Result.result value set the string result of the
            get_config operation

    Raises:
        N/A

    """
    scrapli_conn = task.host.get_connection("scrapli_netconf",
                                            task.nornir.config)
    scrapli_response = scrapli_conn.get_config(source=source,
                                               filters=filters,
                                               filter_type=filter_type)

    result = ScrapliResult(
        host=task.host,
        result=process_command_result(scrapli_response=scrapli_response),
        scrapli_response=scrapli_response,
        changed=False,
    )
    return result