【问题标题】:Why got an unexpected keyword argument 'num_workers' with TypeError: __init__()?为什么使用 TypeError: __init__() 得到一个意外的关键字参数“num_workers”?
【发布时间】:2021-03-25 04:17:51
【问题描述】:
from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.functions import print_result
import json

def prettyPrintDictionary(dict):
  print(json.dumps(dict, indent=2))


nr = InitNornir(config_file="/home/python/nornir-scripts/config.yaml")

print(nr.inventory.hosts)


prettyPrintDictionary(nr.inventory.get_hosts_dict())
# config.yaml
inventory:
  plugin: SimpleInventory
  options:
    host_file: '/home/python/nornir-scripts/inventory/hosts.yaml'
    group_file: '/home/python/nornir-scripts/inventory/groups.yaml'
    defaults_file: '/home/python/nornir-scripts/inventory/defaults.yaml'
runner:
   plugin: threaded
   options:
     num_workers: 100

请帮助我无法理解如何初始化 nornir 我尝试用指南来做,它也有错误。 现在我有这个错误......

【问题讨论】:

  • 您的config.yaml 文件中有什么内容?
  • 它在代码的底部
  • 我有这个 Traceback TypeError: __init__() got an unexpected keyword argument 'num_workers'

标签: python automation napalm


【解决方案1】:

根据documentationnum_workers 键应该在runner.options 键内。将其添加到options 键并重试:

inventory:
  plugin: nornir.plugins.inventory.simple.SimpleInventory
  options:
    host_file: 'inventory/hosts.yaml'
    group_file: 'inventory/groups.yaml'
    defaults_file: 'inventory/defaults.yaml'
runner:
   plugin: threaded
   options:
     num_workers: 100

【讨论】:

  • 我做到了,但仍然有问题(TypeError: __init__() got an unexpected keyword argument 'num_workers'
  • 写下了更改
  • 现在我有这个错误 AttributeError: 'Inventory' object has no attribute 'get_hosts_dict'
  • 写下了更改
  • 在 nornir 3.x 版本中,get_hosts_dict() 已被删除。你需要使用nr.inventory.dict()
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 2017-10-16
  • 2020-10-22
  • 2021-11-17
  • 2016-11-01
  • 2021-10-21
  • 2021-10-15
  • 2021-12-30
相关资源
最近更新 更多