Don’t automate for the sake of automation—automate to simplify, accelerate, and improve.
Note: VCF Automation 8.18.1 was used to create this Blog
I recently received a question about leveraging an Array Input from a VCF Automation Template and processing the array data within an ABX Action. Below, you’ll find example code and screenshots demonstrating the approach. If you’re working with array inputs in your automation workflows, this guide should be helpful!
Screenshots demonstrating the use of VCF Automation Input Arrays:
Example manual Deployment that has an Array Input
Example Custom Form using an Array Input
Screen Shot of the YAML Code for Input Array:
Define the input type as array
Use the array input in the “Cloud.vSphere.Machine” - “properties:” section
All properties values will get passed in the ABX Action
Screen Shot of the Inputs:
If you haven’t worked with YAML array inputs before, use the Inputs screens in VCF Automation to assist in formatting the YAML correctly. This will ensure the data is structured properly before being passed to the ABX Action.
Screen Shot of the Array Creation with Inputs:
Example screen of creating an Array
Screen Shot of the Subscription to execute an ABX Action during an VM Build:
In this example I am running an ABX Action during Pre Allocation of a new VM Build
Screen Shot of the Array Creation with Inputs:
Here’s an example of PowerShell code that takes a YAML array string passed to an ABX Action, cleans it up, and converts it into a PowerShell array
This is the most critical step in this blog post. Take the time to understand how the YAML string is transformed into a PowerShell array, as this enables you to leverage the array values for any automation tasks needed in your server build process.
The PowerShell code processes the YAML string by replacing unnecessary characters, ensuring proper formatting, and then splitting the string on commas to create a PowerShell array. This transformation allows the data to be easily iterated and used within the ABX Action.
Screen Shot of the YAML Array Values:
This example demonstrates how the YAML array format is passed to the ABX Action as a string. Understanding this format is essential for parsing and utilizing the array data effectively within your automation logic.
formatVersion:2name:Rocky-Basicoutputs:__deploymentOverview:value:| **The following vSphere Virtual Machine has been provisioned with VMware VCF Automation.**
**Original Build Specs Used:**
IP: ${input.IP}
Memory(MB): ${input.totalMemoryMB}
CPU (Count): ${input.cpuCount}
Core (Count): ${input.coreCount}
**Connect to Server using Cockpit:**
http://${input.IP}:9090
**Check out these Blogs for Updates/Tips/Tricks on the VMware VCF Operations/Automation Products:**
**Brock Peterson:** https://www.BrockPeterson.com
**Dale Hassinger:** https://www.vCROCS.info
**Cosmin Trif:** https://www.cosmin.gq
**Link to vCROCS VCF Operations:**
https://vao.vcrocs.local
**Link to vCROCS VCF Operations for Logs:**
https://vaol.vcrocs.local #cloud-configinputs:CustomizationSpec:type:stringdescription:Customization Specificationdefault:LINUXtitle:CustomizationSpecVMName:type:stringtitle:VM NameminLength:1maxLength:15default:Rocky-Array-202IP:type:stringdefault:192.168.5.202totalMemoryMB:type:integertitle:Memory(MB)default:1024cpuCount:type:integertitle:CPU (count)default:1coreCount:type:integertitle:Core (count)default:1folderName:type:stringtitle:vCenter Folderdefault:Rocky-Linuxenum:- Rocky-Linux- ESXi-01-VMs- ESXi-02-VMsVolumeGB:type:stringtitle:'Volume Size GB:'default:'20'vmAdminUsers:type:arrayitems:type:stringtitle:'Admin Users:'minItems:1maxItems:4default:- dale.hassingerdescription:VM Admin Usersresources:Network_VMs:type:Cloud.vSphere.Networkproperties:name:PG-VMsnetworkType:existingconstraints:- tag:Network:VMvCenter_Rocky:type:Cloud.vSphere.Machineproperties:image:ROCKY9#flavor: SMtotalMemoryMB:${input.totalMemoryMB}cpuCount:${input.cpuCount}coreCount:${input.coreCount}name:${input.VMName}fqdn:${input.VMName}.vcrocs.localfolderName:Rocky-LinuxvmAdminUsers:${input.vmAdminUsers}storage:bootDiskCapacityInGB:${input.VolumeGB}remoteAccess:authentication:usernamePasswordusername:rootpassword:${secret.administrator}customizationSpec:${input.CustomizationSpec}constraints:- tag:VPZ:VMnetworks:- network:${resource.Network_VMs.id}assignment:staticaddress:${input.IP}
VCF Automation Design Template
Example ABX Action Code:
Code from the screen shots above.
My example code simply outputs the values to demonstrate how to create the array and validate that the code works. Once you have this structure in place, you can extend the foreach section to automate any tasks you need.
Click arrow to expand the ABX Action PowerShell code:
functionhandler($context,$inputs){if(!$inputs.customProperties.vmAdminUsers){$vmAdminUsers="NA"}else{$vmAdminUsers=$inputs.customProperties.vmAdminUsers}Write-Host"STR vmAdminUsers:"$vmAdminUsers# Input string#$vmAdminUsers = '[\"dale.hassinger\",\"brock\",\"chris\"]'# Convert the string into an array$Array=$vmAdminUsers-replace'[\[\\\"\]]',''-split','# Output the array# Iterate through each item in the array and display its valueforeach($Itemin$Array){Write-Host"Username: $Item"}Write-Host"Array:"$ArrayWrite-Host"Array Count:"$Array.Countreturn$inputs}
Lessons learned:
Using Array Inputs are a great way to use multiple values for processes needed to build new VMs.
After you take the YAML array string and turn it into a PowerShell array, you can use the values to automate anything you can script.
vCROCS Deep Dive Podcast
I created a Google NotebookLM Podcast based on the content of this blog. While it may not be entirely accurate, is any podcast ever 100% perfect, even when real people are speaking? Take a moment to listen and share your thoughts with me!
In my blogs, I often emphasize that there are multiple methods to achieve the same objective. This article presents just one of the many ways you can tackle this task. I’ve shared what I believe to be an effective approach for this particular use case, but keep in mind that every organization and environment varies. There’s no definitive right or wrong way to accomplish the tasks discussed in this article.
Lab
Always test new setups and processes, like those discussed in this blog, in a lab environment before implementing them in a production environment.
Tips and Tricks
If you found this blog article helpful and it assisted you, consider buying me a coffee to kickstart my day.