Automation of the initial setup of your Loadbalancer.org appliance (VMware Edition)

Automation of the initial setup of your Loadbalancer.org appliance (VMware Edition)

Load balancer automation Published on 5 mins Last updated

If you are one of those lucky people who have more than one pair of Loadbalancer.org appliances, you might be thinking about buying a Site License, automating the initial setup process. Although our appliances are absolutely awesome and fun to use, going through the same process many times may become slightly tedious! And, personally, I'd rather do something more exciting with my time (and also I'm very lazy ; ) ). Hence I've looked for some tools to help me automate it.

VMware has developed a number of tools that can be used to interact with their product; this includes Python, as well as Go bindings to the vSphere API. In this case I will be using govc which is a CLI tool based based on the latter of the two. There's a whole lot of things that can be done in your vSphere environment using this tool, but the focus of this blog will be on sending keystrokes to the guest operating system, as it can be extremely useful in our case.

The initial setup of your Loadbalancer.org appliance normally must be done using the device's console interface, and it cannot be done remotely via SSH.

Firstly we need to install govc which can be done in many ways. It can be downloaded as a pre built binary:

curl -L -o - "https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname -m).tar.gz" | tar -C /usr/local/bin -xvzf - govc

Or built from source if golang is already installed on your workstation.

go get -u github.com/vmware/govmomi/govc

It's worth mentioning that the above method will work on a Windows Operating System as well. Or you can find the latest pre-compiled binaries here.

Below is an example of the script in Bash that will take care of the initial configuration of your Loadbalancer.org appliance. Please bear in mind it doesn't include deploying the Virtual Machine, and you need to make sure that it is Powered On and prompting for the credentials. Hint: It can be done using govc as well. Please refer to the project documentation for more detail.

#!/bin/sh

# Environment variables specified below are neccessary for govc
# to work. Should you want o use govc outside this script they
# must be sourced in your shell config.

# vSphere Host URL
export GOVC_URL=''

# Your vSphere redentials
export GOVC_USERNAME=''
export GOVC_PASSWORD=''

# Disable certificate validation
# Set to true if using self-signed certificate
export GOVC_INSECURE=true

#
# Variables below are used for the purpose of this script only.
#

# Name of your Loadbalancer.org VM
vm=''

# IP to be used by the node
ip=''

# The netmask in CIDR notation
cidr=''

# Default gateway
gateway=''

# Primary and Secondary DNS
dns1=''
dns2=''

# Your new WebUI password
password=''

# Logging into the configuration wizard
govc vm.keystrokes -vm "${vm}" -s "setup"
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -s "setup"
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# This will reset IP Configuration
# Do you wish to continue? Yes
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Availabe interfaces OK
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
# Do you need to create a bonded interface? No
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Would you like to configure a VLAN? No
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Select interface
# eth0
# Select
govc vm.keystrokes -vm "${vm}" -c KEY_TAB
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Static IP Address
govc vm.keystrokes -vm "${vm}" -s "${ip}"
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
# CIDR prefix

govc vm.keystrokes -vm "${vm}" -s "${cidr}"
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Default Gateway IP Adress
govc vm.keystrokes -vm "${vm}" -s "${gateway}"
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Primary DNS Server
govc vm.keystrokes -vm "${vm}" -s "${dns1}"
govc vm.keystrokes -vm "${vm}" -c KEY_TAB
sleep 1

# Secondary DNS Server
govc vm.keystrokes -vm "${vm}" -s "${dns2}"
govc vm.keystrokes -vm "${vm}" -c KEY_TAB
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Summary
# Configure
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Set Password
# OK
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Password
govc vm.keystrokes -vm "${vm}" -s "${password}"
govc vm.keystrokes -vm "${vm}" -c KEY_TAB
sleep 1

# Password again
govc vm.keystrokes -vm "${vm}" -s "${password}"
govc vm.keystrokes -vm "${vm}" -c KEY_TAB
sleep 1

# WUI and console password set successfully
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1

# Peer Recovery
# No
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
sleep 1
govc vm.keystrokes -vm "${vm}" -c KEY_ENTER
Shell script

It is recommended to wrap the values of the variables in single quotes just to be on the safe side. Most of those should be fine without it but we all love secure password and wrapping those will ascertain that the special characters are not being evaluated. Please bear in mind that not all characters are allowed.

Permitted characters are a-z A-Z 0-9 [ ] # ~ _ * ! = -

Likewise if a name of VM includes spaces this allows for use of those without a need to use an escape character.

# Name of your Loadbalancer.org VM
vm='Awesome Loadbalancer'

# IP to be used by the node
ip='192.168.1.10'

# The netmask in CIDR notation
cidr='24'

# Default gateway
gateway='192.168.1.1'

# Primary and Secondary DNS
dns1='8.8.8.8'
dns2='1.1.1.1'

# Your new WebUI password
password='*s3cur3p4ssw0rd!'
Example variables

Many of the replying lines could be put in the loops in order to shorten the script. I have decided not to do that, so it is more readable to the end user. This script can definitely be improved, depending on the use case (e.g. 'read' could be used in order for the variables values to be provided).

You can possibly notice that after each keypress such as ENTER or TAB the execution is paused for 1 second - sleep 1. This is due to the fact that API is faster than actual key or string send to virtual machine, this little pause makes sure that VM can react to previous keypress before the next one is being send.

As it's mostly a collection of the commands to be executed, it was rather easy to reproduce the same in PowerShell. This requires govc to be installed on your workstation. Note, as mentioned previously, the GitHub repository provides ready pre-built binaries. Another option is to install govc  using a package manager such as Chocolatey:

choco install govc

Bonus

If you are interested in the automation of your systems you might have heard of Ansible. As I mentioned at the beginning Python bindings to vSphere API exist as well and they are used by an Ansible module. If you are Ansible user you can install the module:

ansible-galaxy collection install community.vmware

Installing collection does not install any required third party Python libraries or SDKs. You need to install the required Python libraries using following command.

pip install -r ~/.ansible/collections/ansible_collections/community/vmware/requirements.txt

And then you can run the playbook...

ansible-playbook vcs_loadbalancer_org.yaml --connection=local

....which you can find here!

The playbook makes use of some variables (just like shell script did earlier) as each task requires the values such as hostname, username, password or the name of the VM. You will also notice that keypresses such as ENTER are separate tasks - this is due to design of the Ansible module. If both keys_send and string_send are specified, keys in the keys_send list will be sent in front of the string_send.

    # Variables below are needed by the VMWare module
    vcenter_hostname: 
    vcenter_username: 
    vcenter_password: 
    datacenter_name: 
    vm_name:  
    # Variables below are used to configure your loadbalancer
    ip: 
    cidr: 
    gateway: 
    dns1: 
    dns2: 
    password: 

  tasks:
    - name: Login
      community.vmware.vmware_guest_sendkey:
        validate_certs: false
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ datacenter_name }}"
        name: "{{ vm_name }}"
        string_send: "setup"

    - name: ENTER
      community.vmware.vmware_guest_sendkey:
        validate_certs: false
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ datacenter_name }}"
        name: "{{ vm_name }}"
        keys_send:
        sleep_time: 1
          - ENTER
Ansible playbook excerpt

I hope this gives you some ideas on what is possible, and allows you to create your own solutions!