When creating a custom task in Nornir you may often find yourself using the results of a built-in task of one of the plugins like NAPALM or Netmiko. In my case I simply want to save that result to a variable for use later and don’t really care about the result output that print_result provides. To suppress the result of a sub-task you must add the argument severity_level=logging.DEBUG to the task. You must also import the logging library.
from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.functions import print_result
from nornir.core.filter import F
import logging
def get_napalm_info(task):
configs_result = task.run(name='Get NAPALM configs', task=napalm_get, getters='config', severity_level=logging.DEBUG)
arp_result = task.run(name='Get NAPALM arp', task=napalm_get, getters='arp_table')
def main():
nr = InitNornir(config_file='config.yaml')
#Fitler to Napalm compatible devices
napalm_compatible = nr.filter(F(groups__contains='ios') | F(groups__contains='nxos') | F(groups__contains='eos') | F(groups__contains='junos'))
result = napalm_compatible.run(task=get_napalm_info)
print_result(result)
if __name__ == "__main__":
main()
In this example I have added the argument to the task NAPALM get_config task but not to the NAPALM get_arp_table task. The print_results method will now only provide the ARP table of the devices and not the configuration.
