Skip to main content

Using 'substring' in VCF Automation cloud templates

·3 mins
Automation VMware vExpert VCF Automation Homelab YAML IaC GitLab
Table of Contents
Photo by Tara Evans on Unsplash
I had the opportunity this week to spend some quality time in my happy place in the Homelab this week - VCF Automation / VMware Aria Automation / VMware vRealize Automation. Specifically I was making some cloud templates when I stumbled on a tiny issue with a simple solution that others may trip over. Read on to find out more…

Background
#

As I already mentioned, I was trying to put together a couple of new cloud templates. My preference is to experiment and create the basics using the UI and then copy the YAML code into a GitLab repository. The contents of this repository are imported by VCF Automation and future updates and modifications are made using a code editor and synchronised. I do this because that way my templates are stored outside of VCF Automation and are more portable. I can also make use of different branches in GitLab to manage the progression of changes from development to production. Infrastructure as Code (IaC)!

I wanted the name of the VM resource in the template to be based on the values of some inputs. This is easy to combine with the custom naming feature to add uniqueness to resource names quite easily.

Screenshot of a VCF Automation catalog item with country, location, and environment fields
Figure 1: The catalog item with country, location, and environment input fields.

My aim was to have three inputs (Country, Location, and Environment) shown in Figure 1 above provide the structure of the resource name in the following way:

<country>-<location>-<environment>

With the value for ’environment’ being potentially quite long, I wanted to use only the first character - ‘p’ for ‘production’, ’d’ for ‘development’ etc.

Not to go too far off-piste, but with the initial resource name set to something like ‘uk-red-d’, it can be used by a Custom Naming configuration in VCF Automation like the one below to produce a unique resource name like ‘uk-red-d-002’:

${resource.name}-${###}

Git Import Error
#

With all of that in place I just had to take the possible input values for the environment field, which I was not including in the template itself, but in the Custom Form in Service Broker, and capture the first letter. So at this point the cloud template contained the following:

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# ----- Inputs -----
inputs:
  environment:
    type: string
  country:
    type: string
  location:
    type: string
  photonSettings:
    type: object
    $ref: /ref/property-groups/photonSettings

# ----- Resources -----
resources:
  PhotonServer:
    type: Cloud.vSphere.Machine
    properties:
      name: ${input.country}-${input.location}-${substring(input.environment,0,1)}
      image: ${"photon" + input.photonSettings.photonVersion}
      flavor: ${input.photonSettings.flavorSelection}
      storage:
        bootDiskCapacityInGB: ${input.photonSettings.systemDiskSize}
      networks:
        - network: ${resource.PrimaryNetwork.id}

However, having used the ‘substring’ expression (on line 39) I found that VCF Automation would no longer import the template.

Screenshot of a VCF Automation template import error from GitLab
Figure 2: A template import error in VCF Automation.

begin 0, end 1, length 0

The error text isn’t comprehensive in explaining what the issue is but knowing what I had added to cause it lead me to the answer after a couple of minutes of thinking and a Pilates class. Because the values for the ’environment’ field are provided as part of the custom form and aren’t in the template itself, evaluation of the substring expression fails because environment is empty!

Solution
#

I told you that the solution was simple! Just add a default value to the field in the template:

Screenshot of a VCF Automation template code with default values added for the input fields
Figure 3: Adding default values to the inputs fixed the issue.

Looking back with the problem sorted, the error makes more sense. Hope that helps someone else!

Related

An introduction to vSphere Supervisor and vSphere Kubernetes Service (VKS)
·5 mins
vSphere Supervisor VKS VMware vExpert Homelab Kubernetes
They’ve gone by many names, and I have been talking about them a fair bit, so I decided to do a quick introduction to the vSphere Supervisor and the vSphere Kubernetes Service (VKS).
Handling CA trust with vSphere Supervisor
·2 mins
vSphere Supervisor VMware vExpert Kubernetes TLS SSL Homelab Air Gap Airgapped
In this article I cover off how you can enable SSL trust in the vSphere Supervisor.
Changing the ESXi certificate mode
·2 mins
vSphere VMware vExpert Security ESXi SSL TLS Homelab
In pursuit of trusted SSL everywhere? Haven’t changed the certificates on your ESXi host? Why not, it’s easy!