functions: print_result

print_result is a function that takes a result and prints it. For instance, imagine the following code:

[1]:
from nornir import InitNornir

def dummy_task(task):
    return "hi!!!"

nr = InitNornir(
    inventory={
        "plugin":"YAMLInventory",
        "options": {
            "host_file": "data/hosts.yaml",
            "group_file": "data/groups.yaml",
            "defaults_file": "data/defaults.yaml",
        }
    }
)

result = nr.run(task=dummy_task)

Now we could print the result easily with the print_result function:

[2]:
from nornir_utils.plugins.functions import print_result

print_result(result)
dummy_task**********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv dummy_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
hi!!!
^^^^ END dummy_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv dummy_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
hi!!!
^^^^ END dummy_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv dummy_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
hi!!!
^^^^ END dummy_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv dummy_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
hi!!!
^^^^ END dummy_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev5.no_group ** changed : False *********************************************
vvvv dummy_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
hi!!!
^^^^ END dummy_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Printing specific data

If the task returns different pieces of information you can also select which ones to print. For instance:

[3]:
from nornir.core.task import Result

def task_with_data(task):
    return Result(host=task.host, sw_char="Jar Jar Binks", food="hawaiian pizza", OS="windows")


fav_result = nr.run(task=task_with_data)

print_result(fav_result, vars=["sw_char", "food"]) # print only these vars
task_with_data******************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv task_with_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Jar Jar Binks
hawaiian pizza
^^^^ END task_with_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv task_with_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Jar Jar Binks
hawaiian pizza
^^^^ END task_with_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv task_with_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Jar Jar Binks
hawaiian pizza
^^^^ END task_with_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv task_with_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Jar Jar Binks
hawaiian pizza
^^^^ END task_with_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev5.no_group ** changed : False *********************************************
vvvv task_with_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Jar Jar Binks
hawaiian pizza
^^^^ END task_with_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Severity

You can also ask print_result to print the results only if the severity is equal or superior to the one specified. This is particularly useful if your script is very large and you don’t care about intermediate results. For instance:

[4]:
import logging

def complex_task(task):
    task.run(task=dummy_task, severity_level=logging.DEBUG)
    task.run(task=dummy_task, severity_level=logging.DEBUG)
    task.run(task=dummy_task, severity_level=logging.DEBUG)
    task.run(task=dummy_task, severity_level=logging.DEBUG)
    task.run(task=dummy_task, severity_level=logging.DEBUG)
    return "I did a lot of things!!!"

complex_result = nr.run(task=complex_task)

print_result will only print INFO severity and above by default so if we print the result we should only see the result of the parent task:

[5]:
print_result(complex_result)
complex_task********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev5.no_group ** changed : False *********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you wanted to print all the tasks for debugging purposes you could specify it with:

[6]:
print_result(complex_result, severity_level=logging.DEBUG)
complex_task********************************************************************
* dev1.group_1 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev5.no_group ** changed : False *********************************************
vvvv complex_task ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
I did a lot of things!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
---- dummy_task ** changed : False --------------------------------------------- DEBUG
hi!!!
^^^^ END complex_task ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^