How to: Use Ubiquity Cloud User Data Variables

Introduction

Our development team has made several user data variables available for usage within the customizable cloud-config scripts on our cloud platform. The implementation of these variables into your cloud-config scripts provides access to otherwise unknown variables in the deployment process, which enables further automation possibilities on our platform.

User data variables can be utilized in one of two ways: by adding them to your cloud-config(s) within the Update User Data feature on our cloud order form or by including them in the user data parameter of our API’s cloud.create function. Below, we will detail the values that will be returned when setting these variables in your cloud-config scripts, and additionally provide an example implementation utilizing several of these variables.

User Data Variables

The majority of user data variables made available are limited in scope to the network configuration settings of the cloud instance being deployed. However, our developers have provided a few user account variables as well that may benefit you depending on your intended objective(s).

Each user data variable is delimited by two percentage signs at the beginning and end of the variable name, which allows them to be correctly interpreted as valid user data variables on our cloud platform. For a breakdown of each variable and an example use case, continue reading.

User Variables

%%root_user%% – Returns ‘root’ for any Linux-based operating systems.

%%root_pass_hash%% – Returns the root password in hash format.

%%root_pass%% – Returns the randomly generated root/administrator password.

%%email%% – Returns the primary email address associated with the account.

Network Variables

%%hostname%% – Returns the hostname set during the order process.

%%defaultgateway%% – Returns the default route in which network traffic passes through.

Private Network Variables

%%private_ip%% – Returns the private network address assigned.

%%private_subnet_prefix%% – Returns the private network address CIDR prefix number.

%%private_subnet_netmask%% – Returns the private network subnet mask for the private IP address.

%%private_subnet_gateway%% – Returns the private network gateway address.

%%private_subnet_network%% – Returns the private network address.

%%private_subnet_broadcast%% – Returns the private network broadcast address.

Public Network Variables

%%public_ip%% – Returns the public IP address assigned.

%%public_subnet_prefix%% – Returns the public network address CIDR prefix number.

%%public_subnet_netmask%% – Returns the public network subnet mask for the public IP address.

%%public_subnet_gateway%% – Returns the public network gateway address.

%%public_subnet_network%% – Returns the public network address.

%%public_subnet_broadcast%% – Returns the public network broadcast address.

Example Variable Outputs
%%root_user%% => root
%%root_pass%% => @e0X7txXNVNGzfu
%%root_pass_hash%% => /tHxD1snzdEcU1obSTiJ3cFLND0XT2Tor8uU9vVYNDgc4wM9EM.za/7E3SpjgQW0at/
%%email%% => testaccount@gmail.com

%%hostname%% => MyCloudHostname
%%defaultgateway%% => 10.6.80.1

%%private_subnet_prefix%% => 24
%%private_ip%% => 10.6.80.48
%%private_subnet_netmask%% => 255.255.255.0
%%private_subnet_gateway%% => 10.6.80.1
%%private_subnet_network%% => 10.6.80.0
%%private_subnet_broadcast%% => 10.6.80.255

%%public_subnet_prefix%% => 22
%%public_ip%% => 23.108.11.3
%%public_subnet_netmask%% => 255.255.252.0
%%public_subnet_gateway%% => 23.108.8.1
%%public_subnet_network%% => 23.108.8.0
%%public_subnet_broadcast%% => 23.108.11.255

Example Cloud-config Utilizing User Data Variables

The cloud-config provided below is the default for our CentOS 7.x template with the addition of installation and configuration file updates for Apache added to the runcmd section. After the Apache web server has been installed, we use the sed file/stream editor to find and update two default configuration parameter lines in /etc/httpd/conf/httpd.conf: ServerName and ServerAdmin. The ServerAdmin line is updated using the %%email%% user data variable while the ServerName line is updated to the %%hostname%% user data variable. By doing so, we avoid having to manually update these files and automate a customized Apache web server configuration.

#cloud-config
ssh_pwauth: True
manage_etc_hosts: True
write_files:
  - path: /etc/sysconfig/network-scripts/ifcfg-eth0
    permissions: '0644'
    owner: root:root
    content: |
      DEVICE=eth0
      TYPE=Ethernet
      IPADDR=%%public_ip%%
      NETMASK=%%public_subnet_netmask%%
      DNS1=8.8.8.8
      DNS2=8.8.4.4
      ONBOOT=yes
      DEFROUTE=yes
      BOOTPROTO=static
      GATEWAY=%%public_subnet_gateway%%
      NM_CONTROLLED=no
      MTU=1500
  - path: /etc/sysconfig/network-scripts/ifcfg-eth1
    permissions: '0644'
    owner: root:root
    content: |
      DEVICE=eth1
      TYPE=Ethernet
      IPADDR=%%private_ip%%
      NETMASK=%%private_subnet_netmask%%
      ONBOOT=yes
      BOOTPROTO=static
      NM_CONTROLLED=no
      MTU=1450
  - path: /etc/sysconfig/network-scripts/route-eth0
    permissions: '0644'
    owner: root:root
    content: |
      169.254.169.254/32 via %%dhcp_ip_public%% dev eth0
runcmd:
 - echo 'root:%%root_pass_hash%%' | chpasswd -e
 - ip route flush table all
 - ifdown eth0; ifdown eth1
 - ifup eth0; ifup eth1
 - yum clean all
 - yum -y update
 - yum install httpd -y
 - sed -i "s/#ServerName www.example.com:80/ServerName %%hostname%%/g" /etc/httpd/conf/httpd.conf
 - sed -i "s/ServerAdmin root@localhost/ServerAdmin %%email%%/g" /etc/httpd/conf/httpd.conf
 - systemctl restart httpd

Written by
on December 30, 2015

Facebook Twitter Google+ LinkedIn Addthis