Search

Photon OS as your Infrastructure Automation hub!!

Updated: Dec 14, 2021



The VMware photon OS minimal version has inbuilt docker container support. This, in addition to the vmware/powerclicore docker image: vmware/powerclicore - Docker Image | Docker Hub, allows us to build a very lightweight automation gateway/ hub for carrying out our regular infrastructure automation tasks, one of which I will be discussing later in this post.

William Lam has written a great blog post on preparing Linux OVAs with customization scripts execution during first boot. Visit his blog post: Building your own Virtual Appliances using OVF properties Part 2 (williamlam.com), for more details on this. I will be covering some aspects of building the PhotonOS OVA in line with the requirement of my use case.


To start with, we first download the official Photon OS 4.0 OVA from the below link:

Downloading Photon OS · vmware/photon Wiki (github.com).

Deploy the OVA in your environment, the OVA does not ask for any customization parameters, so it is a pretty straight forward process. Follow the instructions to change the root password of your Photon VM. Ensure you have net connectivity in your environment. Power up the VM and log in using root account. Enter the following command:

Once this is complete, power off the VM and follow the instructions as per William Lam's post to setup the VM's OVF properties. Refer to his github repository "custom-virtual-appliances/rc.local at master · lamw/custom-virtual-appliances · GitHub" for the complete rc.local script.

Add the below lines to the end of the rc.local script. This script will first replace 'x', 'y' and 'z' in test.ps1 script and then execute the test.ps1 script during the boot up process.

	
   #########################
   ### Run PowerCli Jobs ###
   #########################
	sed -i -e "s/X/${VCENTER}/g" /PS/test.ps1
	sed -i -e "s/Y/${USER}/g" /PS/test.ps1
	sed -i -e "s/Z/${PASS}/g" /PS/test.ps1
	systemctl start docker
	systemctl start containerd
	docker run -d --name=CLI --entrypoint="/usr/bin/pwsh" -v /PS:/tmp/scripts vmware/powerclicore /tmp/scripts/test.ps1
	docker wait CLI
	docker rm --force CLI
	sleep 10
	shutdown now

##########
###END####
##########

The script enables us to execute PowerCLI scripts during the VM's first boot. In addition, the VM shuts down after the task is complete. This way, we can power up the VM only when we need to execute a particular automated task and once the task is complete the VM shuts down.

The script also allows us to replace customization variables in our PowerCLI script. The variables must be added to the OVF environment properties when creating the OVA and rc.local script. See the sample test.ps1 script below. The modified rc.local replaces "X", "Y", "Z" with customization values:

# ENVIRONMENT VARIABLES, TO BE REPLACED BY rc.local SCRIPT
$vcenter="X"
$user="Y"
$pass="Z"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer-Server $vcenter-User $user-Password $pass|Out-Null
##SAMPLE SCRIPT HERE#####THIS SECTION CAN BE USED FOR WRTING ANY SCRIPT OR CALL ONTO OTHER SRIPTS##
Get-VM-Name "Dummy1"|Stop-VM-Confirm:$false

If you want to execute multiple scripts, you can use the exact same rc.local and within your test.ps1 you can call onto the execution of the other scripts:


#
# Sample test.ps1 to call multiple scripts
#

# Script1
& ./script1.ps1

# Script2
 & ./script2.ps1

# Script3
& ./script3.ps1

Once the OVF properties are set, power-up the VM. Copy the rc.local to /etc/rc.d and the powershell scripts to /PS. Once the files are copied, run the commands as shown below:

These commands set executable permission for our scripts and optimizes the OVA size. Once these commands are entered, power off the VM and export as OFV template. Incase you want to convert the OVF files into an OVA, use the vmware OVFtools.


The screencap below shows the PhotonOS VM executing a PowerCLI script during bootup to poweroff dummy VM 1 and then itself shuts down:




64 views0 comments

Recent Posts

See All