bash 将 IP 地址传递给 cloud-init 元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34507807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:03:56  来源:igfitidea点击:

Pass IP-address to cloud-init metadata

linuxbashopenstackkvmcloud-init

提问by Jonas Libbrecht

I am searching for a way to pass a ip-address to cloud-init metadata. So when my qcow boots, it does not have to wait for 120 - 180 seconds to boot.

我正在寻找一种将 ip 地址传递给 cloud-init 元数据的方法。所以当我的 qcow 启动时,它不必等待 120 - 180 秒来启动。

Currently I've created a workaround by adding IP-address information to the userdata part of cloud-init. The con is, it does take some time because cloud-init userdata is only executed after booting the VM.

目前我已经通过将 IP 地址信息添加到 cloud-init 的用户数据部分来创建一个解决方法。缺点是,它确实需要一些时间,因为 cloud-init userdata 仅在启动 VM 后执行。

echo -e "
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
\taddress $address
\tnetmask $netmask
\tgateway $gateway
\tdns-nameservers $dnsnameservers
" > /etc/network/interfaces

ifdown eth0; ifup eth0

Currently to set the hostname in cloud-init metadata, I've already obtained this part:

目前要在 cloud-init 元数据中设置主机名,我已经获得了这部分:

cat > $config_dir/meta-data <<-EOF
instance-id: $uuid
hostname: $hostname
local-hostname: $hostname
EOF

But I need something more solid, more concrete because the cloud-init documentation is very vague

但我需要更可靠、更具体的东西,因为 cloud-init 文档非常模糊

EDIT:I've seen that it is possible because Openstack can do it.

编辑:我已经看到这是可能的,因为 Openstack 可以做到。

回答by codebauss

It is possible by using the following format:

可以使用以下格式:

network-interfaces: |
  auto lo
  iface lo inet loopback

  iface eth0 inet static
    address xxx.xxx.xxx.xxx (static ip)
    netmask xxx.xxx.xxx.xxx
    gateway xxx.xxx.xxx.xxx (gateway ip, usually ip address of router)

You basically write out the configuration you want your interfaces to have as an interfaces file would look as long as you have 'network-interfaces: |'

您基本上写出了您希望接口具有的配置,因为只要您有“网络接口:|”,接口文件就会看起来像

Hope that answers your question!

希望这能回答你的问题!