Pass IP-address to cloud-init metadata

Jonas Libbrecht picture Jonas Libbrecht · Dec 29, 2015 · Viewed 9.8k times · Source

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.

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.

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:

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

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

Answer

codebauss picture codebauss · Dec 29, 2015

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!