Kyle Pericak

"It works in my environment."

Sat 25 January 2020

OpenStack: Execute Powershell from Cloud-Init

Posted by Kyle Pericak in cloud   

This post is linked to from the OpenStack Deep Dive Project


When you launch an instance in OpenStack, you can provide a script to cloud-init that will be executed at startup time. The glance template needs to have cloud-init installed (linux) or Cloudbase-init (windows).

Write your Powershell Script

You need to define the script ahead of time. Here's a super simple script to add a local administrator:

#ps1
$name = "MyUser"
$password = "MyPassword"
$password_secure_string = ConvertTo-SecureString -AsPlainText -Force $password
$new_user = New-LocalUser -Name $name -Password $password_secure_string -AccountNeverExpires
Add-LocalGroupMember -Group "Administrators" -Member $new_user

Create the Instance

There's more than one way to do just about everything, but here's how I do it.

# source openrc file
source my-openrc.sh

# Collect environment details
openstack flavor list
openstack network list
openstack image list

# Set the VM specs
flavor=""
network=""
image=""
name="CloudInitDemo"
size=60
vol_name="$name-boot"

# Create a boot volume
openstack volume create --image $image --bootable --size $size $vol_name

# create server and specify cloud-init script
script_file="/home/kyle/localAdmin.ps1"
openstack server create \
  --volume $vol_name \
  --flavor $flavor \
  --network $network \
  --user-data $script_file \
  $name

Javascript appears to be disabled. Comments can't load.