从 Nornir 2.3.0 隐蔽到 Nornir 3.2.0

发布于 2025-01-10 04:22:10 字数 1548 浏览 0 评论 0原文

需要从 shell 输入获取用户名和密码

def adapt_host_data(host):
    host.username = Gusername
    host.password = Gpassword

if __name__ == "__main__":
    Gusername = input('Username: ')
    Gpassword = getpass('Password: ')

    nr = InitNornir(
    inventory={
                "plugin": "nornir.plugins.inventory.simple.SimpleInventory",
                "options": {
                    "host_file": "./inventory/hostise.yaml",
                    "group_file": "./inventory/groups.yaml"
                            },
                "transform_function": adapt_host_data,
            },)

这在 Nornir 2.3.0 下工作正常。但我无法在 Nornir 3.2.0 下使用它。 请帮助

这是我得到的错误

Traceback (most recent call last):
  File "filesname.py", line 70, in <module>
    "transform_function": adapt_host_data,
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/init_nornir.py", line 72, in InitNornir
    inventory=load_inventory(config),
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/init_nornir.py", line 19, in load_inventory
    inventory_plugin = InventoryPluginRegister.get_plugin(config.inventory.plugin)
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/core/plugins/register.py", line 76, in get_plugin
    raise PluginNotRegistered(f"plugin {name!r} is not registered")
nornir.core.exceptions.PluginNotRegistered: plugin 'nornir.plugins.inventory.simple.SimpleInventory' is not registered

Need to get username and password from shell input

def adapt_host_data(host):
    host.username = Gusername
    host.password = Gpassword

if __name__ == "__main__":
    Gusername = input('Username: ')
    Gpassword = getpass('Password: ')

    nr = InitNornir(
    inventory={
                "plugin": "nornir.plugins.inventory.simple.SimpleInventory",
                "options": {
                    "host_file": "./inventory/hostise.yaml",
                    "group_file": "./inventory/groups.yaml"
                            },
                "transform_function": adapt_host_data,
            },)

This is working fine under Nornir 2.3.0. But iam not able to use this under Nornir 3.2.0.
Please help

This is the error i get

Traceback (most recent call last):
  File "filesname.py", line 70, in <module>
    "transform_function": adapt_host_data,
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/init_nornir.py", line 72, in InitNornir
    inventory=load_inventory(config),
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/init_nornir.py", line 19, in load_inventory
    inventory_plugin = InventoryPluginRegister.get_plugin(config.inventory.plugin)
  File "/heim/tahir/scripts/Python/nornir/envNornir3/lib64/python3.6/site-packages/nornir/core/plugins/register.py", line 76, in get_plugin
    raise PluginNotRegistered(f"plugin {name!r} is not registered")
nornir.core.exceptions.PluginNotRegistered: plugin 'nornir.plugins.inventory.simple.SimpleInventory' is not registered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2025-01-17 04:22:10

需要注册 InventoryPluginRegister 和 SimpleInventory

def transform_func(host):
    host.username = Gusername
    host.password = Gpassword

if __name__ == "__main__":

    from nornir.core.plugins.inventory import InventoryPluginRegister
    from nornir.plugins.inventory import SimpleInventory
    from nornir.core.plugins.inventory import TransformFunctionRegister
    InventoryPluginRegister.register("SimpleInventory", SimpleInventory)
    TransformFunctionRegister.register("transform_func", transform_func)

    Gusername = input('Username: ')
    Gpassword = getpass('Password: ')
#
    nr = InitNornir(config_file="./inventory/config.yaml")

并且 config.yaml 必须如下所示

inventory:
  plugin: SimpleInventory
  transform_function: transform_func
  options:
    host_file: "./inventory/hostise.yaml"
    group_file: "./inventory/groups.yaml"
    defaults_file: "./inventory/defaults.yaml"

logging:
  enabled: False

Needed to register InventoryPluginRegister and SimpleInventory

def transform_func(host):
    host.username = Gusername
    host.password = Gpassword

if __name__ == "__main__":

    from nornir.core.plugins.inventory import InventoryPluginRegister
    from nornir.plugins.inventory import SimpleInventory
    from nornir.core.plugins.inventory import TransformFunctionRegister
    InventoryPluginRegister.register("SimpleInventory", SimpleInventory)
    TransformFunctionRegister.register("transform_func", transform_func)

    Gusername = input('Username: ')
    Gpassword = getpass('Password: ')
#
    nr = InitNornir(config_file="./inventory/config.yaml")

And the config.yaml have to look like this

inventory:
  plugin: SimpleInventory
  transform_function: transform_func
  options:
    host_file: "./inventory/hostise.yaml"
    group_file: "./inventory/groups.yaml"
    defaults_file: "./inventory/defaults.yaml"

logging:
  enabled: False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文