Automation of the initial setup of your Loadbalancer.org appliance (VMware Edition)
Automation Published on •5 mins Last updatedIf 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.
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.
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
.
I hope this gives you some ideas on what is possible, and allows you to create your own solutions!