ArchiveAbout

Kyle Pericak

"It works in my environment"

Created: 2020-01-25Updated: 2020-01-25

OpenStack: Execute Powershell from Cloud-Init

Category:cloudTags:openstackwindowspowershell;
Running startup scripts on Windows instances deployed by OpenStack

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
Tags
ansible
Blog code last updated on 2024-02-18: 5ab386de2324c1884556552d0f043a42f2f726ab