-2

我有一个基本问题。我使用 Nornir 触发 Netmiko 来配置 Cisco 路由器。它仅在我对主机清单(主机名)中的 IP 地址进行硬编码时才有效。

Europe-Cisco-Site1:
  hostname: "" <-- when I put the public IP here, it works!
  username: "ec2-user"
  password: ""
  platform: "cisco_xe"
  groups:
    - cisco
  connection_options:
    netmiko:
      extras:
        use_keys: true
        key_file: "/home/coder/.ssh/id_rsa"

就我而言,我必须从 AWS 获取公共 IP,然后动态分配主机名变量:

def assign_aws_public_ips(task):
    task.host['hostname'] = get_aws_public_ip(str(task.host), task.host['aws_region'])
    print("****************************")
    print(task.host['hostname'])
    print("****************************")
    result = task.run(
        task=netmiko_send_config,
        config_file="templates/simple.j2"
    )
    print_result(result)
    return(f"{task.host} = {task.host['hostname']}")

如果在主机名上分配了公共 IP,这只是一个要验证的测试脚本:

****************************
**52.59.216.193** <-- this is the public IP which has been assigned to **hostname**
****************************
netmiko_send_config*************************************************************
* Europe-Cisco-Site1 ** changed : False ****************************************
vvvv netmiko_send_config ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Traceback (most recent call last):
  File "/home/coder/.local/lib/python3.8/site-packages/nornir/core/task.py", line 99, in start
    r = self.task(self, **self.params)
  File "/home/coder/.local/lib/python3.8/site-packages/nornir_netmiko/tasks/netmiko_send_config.py", line 24, in netmiko_send_config
    net_connect = task.host.get_connection(CONNECTION_NAME, task.nornir.config)
  File "/home/coder/.local/lib/python3.8/site-packages/nornir/core/inventory.py", line 494, in get_connection
    self.open_connection(
  File "/home/coder/.local/lib/python3.8/site-packages/nornir/core/inventory.py", line 546, in open_connection
    conn_obj.open(
  File "/home/coder/.local/lib/python3.8/site-packages/nornir_netmiko/connections/netmiko.py", line 59, in open
    connection = ConnectHandler(**parameters)
  File "/home/coder/.local/lib/python3.8/site-packages/netmiko/ssh_dispatcher.py", line 326, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/home/coder/.local/lib/python3.8/site-packages/netmiko/cisco/cisco_ios.py", line 17, in __init__
    return super().__init__(*args, **kwargs)
  File "/home/coder/.local/lib/python3.8/site-packages/netmiko/base_connection.py", line 244, in __init__
    raise ValueError("Either ip or host must be set")
ValueError: Either ip or host must be set

问: 为什么Netmiko不用播放时分配的主机名?它总是采用未在主机清单文件中定义的主机名?

一个糟糕的解决方案是使用公共 IP 更新主机清单文件,但这确实是一个糟糕的解决方案,我无法想象应该这样做!?

4

1 回答 1

0

现在明白了!!!!错过了一些基本的东西,虽然我可以像字典一样使用主机名,但这不是真的。谢谢您的帮助!

这是正确的代码:

def assign_aws_public_ips(task):
    task.host.hostname = get_aws_public_ip(str(task.host), task.host['aws_region'])
    return(f"{task.host} = {task.host.hostname}")

这是描述众所周知的属性的正确教程,供参考: https ://gist.github.com/danielmacuare/c647880cfc99a605d25c3b669ab63fc7

于 2021-08-27T14:36:48.393 回答