Changing the instance hostname with a workflow/task

Hey @Alex,

So you can utilize the “Configuration Phase”, which triggers calls before the payload is submitted.

  1. Create this Groovy task:
    println("${spec.encodeAsJson().toString()}")

  2. Put that task in a provisioning workflow under the “Configuration Phase”.

  3. Deploy a new VM with that workflow selected. The configuration phase will run and task output will show the type of data you have available.

Essentially, there is a variable spec that is available in this phase. You can modify that JSON payload, and then output it.

Note, you must have your last configuration phase task only output the spec JSON, no other stdout. You must also set the Result Type of the task to “JSON”.

A few examples in the forums:

And an example PowerShell Core code I use. This:

  1. Requests an IP from an IP Pool via the Morpheus API.
  2. Sets a temp name on the request for the IP.
  3. Updates the VM hostname based on the IP address hash.
  4. Echoes out the spec JSON body to pick the changes up.
$configJson = '<%=spec.encodeAsJson().toString()%>'
$bearer = "<%=morpheus.apiAccessToken%>"
$morphUrl = '<%=morpheus.applianceUrl%>'
$ContentType = 'application/json'
$morphHeader = @{
    "Authorization" = "BEARER $bearer"
}
$poolApi = 'api/networks/pools/'
$configJson = $configJson | convertfrom-json -Depth 10
$pool = $configJson.networkInterfaces.network.pool.id

### Functions
function ipv4ToHexString($ipv4){
 
    # Validation
    $valid = $true
    $ipv4.split(".") | foreach { if([int]$_ -ge 0 -and [int]$_ -le 255) { } else { $valid = $false } }
 
    if($valid -eq $true){
 
        # Conversion
        $hexString = ""
        $hexList = @("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")
        $ipv4.split(".") | foreach {
            $mod = [int]$_ % 16
            $first = $hexList[(([int]$_ - $mod) / 16)]
            $second = $hexList[$mod]
            $hexString += $first+$second
        } 
        return $hexString
    }
    else{
        return $false
    }
}

#################################################################################
### Script
$tempName = get-random
$poolBody = @"
{
    "createDns": false,
    "networkPoolIp": {
      "hostname": $tempName
    }
}
"@

# Request IP
$ip = (Invoke-WebRequest -Method POST -Uri ($morphUrl + $poolApi + $pool + '/ips') -SkipCertificateCheck -Headers $morphHeader -Body $poolBody -ContentType $ContentType).content | convertfrom-json
$ip = $ip.networkPoolIp

# Generate Name Hash
$hashName = (ipv4ToHexString -ipv4 $ip.ipAddress).toUpper()
$hashName = "$hashName"

$configJson.instance.name = $hashName
$configJson.instance.hostName = $hashName

$configJson = $configJson | ConvertTo-Json -Depth 10

$spec = @"
{
    "spec": $configJson
}
"@

$spec

#spec #configurationphase

1 Like