Openstack HOT, automatically setup static IP address to instance
It is possible to set up static IP using Openstack HOT (Heat Orchestration template), but it need to have 2 network attached. First interface to retrieve setting from cloud init injection and second interface is the one that will be use as main interface. First interface is local network that connected to internet. After instance is created, detach first interface with local network and then reboot the instance. After that, we can ssh to the server using the static IP address. Basically, this HOT, create instance with 2 interface and then change the interface configuration using script defined in the runcmd section.
heat_template_version: 2016-04-08
parameters:
key_name:
type: string
default: default
server_name:
type: string
default: server-name
description: instance name
flavor:
type: string
default: flavor1
constraints:
- allowed_values: [flavor1, flavor2]
image:
type: string
default: centos
constraints:
- allowed_values: [centos, debian, ubuntu, opensuse, fedora]
provider_net_id:
type: string
default: 123af45-ac98-411c-d698-ec88b37dd093
label: external network id
private_net_name:
type: string
description: private-network
default: private-net
root_pw2:
type: string
description: root password
default: root_password
user_pw:
type: string
description: user password
default: user_password
gw_ip:
type: string
description: ip gateway
netmask:
type: string
description: netmask of the network
dns:
type: string
description: dns server
default: 1.1.1.1
resources:
instance1_port1:
type: OS::Neutron::Port
properties:
admin_state_up: true
network_id: { get_param: provider_net_id }
security_groups:
- default
instance1:
type: OS::Nova::Server
properties:
name: { get_param: server_name }
image: { get_param: image }
flavor: { get_param: flavor }
availability_zone: zone1
networks:
- network: {get_param: private_net_name}
- port: { get_resource: instance1_port1 }
user_data_format: RAW
user_data:
str_replace:
template: |
#cloud-config
final_message: "The system is finally up, after $UPTIME seconds"
ssh_pwauth: True
disable_root: false
users:
- name: user
groups: users
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
lock_passwd: false
chpasswd:
list: |
user:$CLOUDPW
root:$ROOTPW
expire: False
runcmd:
- "if egrep 'rhel|centos|fedora|suse' /etc/os-release; then ifdown eth0; if egrep 'suse' /etc/os-release; then ifdir='/etc/sysconfig/network'; else ifdir='/etc/sysconfig/network-scripts'; fi; rm -f $ifdir/ifcfg-eth0; echo 'Delete ifcfg-eth0'; elif egrep 'debian|ubuntu' /etc/os-release; then rm -f /etc/network/interfaces.d/50-cloud-init.cfg; echo 'network: {config: disabled}' > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg; else echo 'OS NOT DETECTED';exit 1;fi;"
- "if egrep 'rhel|centos|fedora' /etc/os-release; then ifdir='/etc/sysconfig/network-scripts'; macadd=$(ip -o link | grep eth1 | cut -d/ -f2 | awk {'print $2'});if [ ! -e ${ifdir}'/ifcfg-eth0' ];then echo 'Creating iface file for ifcfg-eth0';echo -e 'TYPE=Ethernet\nIPADDR='$PROVIP'\nHWADDR='$macadd'\nPREFIX=26\nDEFROUTE=yes\nDEVICE=eth0\nONBOOT=yes\nGATEWAY='$GWIP'\nDNS1='$DNS > $ifdir'/ifcfg-eth0'; ifup eth0; fi; elif egrep 'debian|ubuntu' /etc/os-release; then if egrep 'ID=ubuntu' /etc/os-release; then theline=2; else theline=2; fi; ifname0=$(ip -o link | awk 'NR=='$theline | cut -d: -f2 | awk {'print $1'}); ifdown $ifname0; ifdir='/etc/network/interfaces';if [ -e $ifdir ];then echo 'Add '$ifname0' to '$ifdir;echo 'auto lo\niface lo inet loopback\n\nauto '$ifname0'\niface '$ifname0' inet static\naddress '$PROVIP'\nnetmask '$NETMASK'\ngateway '$GWIP'\ndns-nameservers '$DNS > $ifdir; ifup $ifname0; fi; elif egrep 'suse' /etc/os-release; then if egrep 'sles' /etc/os-release; then theline=2; else theline=3; fi; ifdir='/etc/sysconfig/network'; ifname0=$(ip -o link | awk 'NR=='$theline | cut -d: -f2 | awk {'print $1'}); ifdown $ifname0; echo 'Add '$ifname0' to '$ifdir; echo 'STARTMODE=auto\nBOOTPROTO=static\nIPADDR='$PROVIP'/26\nUSERCONTROL=no\nNM_CONTROLLED=no\nBRIDGE=no' > $ifdir'/ifcfg-'$ifname0; echo 'default '$GWIP' - -' > /etc/sysconfig/network/routes; echo 'nameserver '$DNS >> /etc/resolv.conf; ifup $ifname0; else echo 'OS NOT DETECTED'; exit 1; fi;"
- "sed -i '/PasswordAuthentication no/d' /etc/ssh/sshd_config"
params:
$PROVIP: { get_attr: [ instance1_port1, fixed_ips, 0, ip_address ] }
$ROOTPW: { get_param: root_pw2 }
$CLOUDPW: { get_param: cloud_pw }
$GWIP: { get_param: gw_ip }
$NETMASK: { get_param: netmask }
$DNS: { get_param: dns }
outputs:
instance1_static_ip:
description: Fixed IP address of port.
value: { get_attr: [ instance1_port1, fixed_ips, 0, ip_address ] }
Comments