Skip to content

print_structured_result

nornir_scrapli.functions.print_structured_result

print_structured_result(result: AggregatedResult, failed: bool = False, severity_level: int = logging.INFO, parser: str = 'textfsm', to_dict: bool = True, fail_to_string: bool = False) -> None

Prints the :obj:nornir.core.task.Result from a previous task to screen

Parameters:

Name Type Description Default
result AggregatedResult

Nornir AggregateResult object from a previous task

required
failed bool

if True assume the task failed

False
severity_level int

Print only errors with this severity level or higher

logging.INFO
parser str

textfsm|genie -- parser to parse output with

'textfsm'
to_dict bool

output structured data in dict form instead -- basically put k:v instead of just lists of lists of values for textfsm output; ignored if parser == "genie"

True
fail_to_string bool

fallback to printing unstructured output or have tasks skipped (because print_result won't print empty lists which scrapli returns if parsing fails)

False
Source code in functions/print_structured_result.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def print_structured_result(
    result: AggregatedResult,
    failed: bool = False,
    severity_level: int = logging.INFO,
    parser: str = "textfsm",
    to_dict: bool = True,
    fail_to_string: bool = False,
) -> None:
    """
    Prints the :obj:`nornir.core.task.Result` from a previous task to screen

    Arguments:
        result: Nornir AggregateResult object from a previous task
        failed: if `True` assume the task failed
        severity_level: Print only errors with this severity level or higher
        parser: textfsm|genie -- parser to parse output with
        to_dict: output structured data in dict form instead -- basically put k:v instead of just
            lists of lists of values for textfsm output; ignored if parser == "genie"
        fail_to_string: fallback to printing unstructured output or have tasks skipped (because
            print_result won't print empty lists which scrapli returns if parsing fails)

    """
    updated_agg_result = AggregatedResult(result.name)
    for hostname, multi_result in result.items():
        updated_multi_result = MultiResult(result.name)
        for individual_result in multi_result:
            scrapli_responses = getattr(individual_result, "scrapli_response", None)
            if isinstance(scrapli_responses, Response):
                scrapli_responses = [scrapli_responses]
            if not scrapli_responses:
                updated_multi_result.append(individual_result)
                continue
            for scrapli_response in scrapli_responses:
                parser_method = getattr(scrapli_response, f"{parser}_parse_output")
                updated_result = Result(
                    host=individual_result.host,
                    changed=individual_result.changed,
                    diff=individual_result.diff,
                    exception=individual_result.exception,
                    failed=individual_result.failed,
                    name=individual_result.name,
                    severity_level=individual_result.severity_level,
                    stderr=individual_result.stderr,
                    stdout=individual_result.stdout,
                )

                if parser == "textfsm":
                    structured_result = parser_method(to_dict=to_dict)
                else:
                    structured_result = parser_method()

                if not structured_result and fail_to_string:
                    updated_result.result = scrapli_response.result
                else:
                    updated_result.result = structured_result
                updated_multi_result.append(updated_result)
        if updated_multi_result:
            updated_agg_result[hostname] = updated_multi_result  # noqa

    LOCK.acquire()  # pylint: disable=R1732
    try:
        _print_result(
            result=updated_agg_result, attrs=None, failed=failed, severity_level=severity_level
        )
    finally:
        LOCK.release()