为什么 Netmiko 不使用动态分配的(主机名)IP 地址进行连接?

Why Netmiko does not use the dynamically assigned (hostname) IP address for connection?

我有一个基本问题。我使用 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 获取 public 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']}")

这只是一个用于验证的测试脚本,如果 public 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 不使用播放过程中分配的主机名?它总是采用主机清单文件中未定义的主机名?

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

知道了!!!!真的错过了一些基本的东西,虽然我可以像字典一样使用主机名,但事实并非如此。感谢您的帮助!

这是正确的代码:

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