Dynamically added Target in Step1 is not available in Step2

Hi Team,
I am adding a dynamic Target to the Infrastructure using Octopus APIs in Step1 and then wants to execute a script on this target in Step2, however the target is registered successfully in Step1 but Step2 can not find it. Is there any way to find the dynamic target added in Step1 in the Step2?

Step1 Source code is as below -

$ipAddress = "10.xx.xx.xx"


Write-Host "IP Address: - $ipAddress"

$RoleName = "Test"

if([string]::IsNullOrEmpty($ipAddress)) {
   Write-Host "IP Address is empty"
   exit 
}

$certificateThumbprint = "xxxx"

#This is Salty's API Key
$apiKey = "API-xxxxx"
$apiUrl = "$($OctopusParameters["Octopus.Web.ServerUri"].TrimEnd('/'))/api"


$environmentName = $OctopusParameters["Octopus.Environment.Name"] 
$spaceName = "Test"
$headers = @{ "X-Octopus-ApiKey" = $APIKey; }

$environments = Invoke-RestMethod -Uri "$OctopusBaseUrl/environments?partialName=$([uri]::EscapeDataString($environmentName))&skip=0&take=100" -Headers $headers -ErrorVariable octoError

$matchingEnvironment = $environments.Items | Where-Object { $_.Name -eq "$environmentName" }

$headers = @{ "X-Octopus-ApiKey" = $APIKey; }

function New-TentacleUri() {
	param(
		[Parameter(Mandatory=$true)][string]$ipAddress
	)
		return "https://$($ipAddress):10933/"
}

function New-TentacleEndpoint() {
	param(
		[Parameter(Mandatory=$true)][string]$ipAddress,
		[Parameter(Mandatory=$true)][string]$CertificateThumbprint
	)
		return @{
			CommunicationStyle = "TentaclePassive";
			Thumbprint = $CertificateThumbprint;
			Uri = "https://$($ipAddress):10933/";			
	}
}

function Remove-DeploymentTargets() {
	param(
		[Parameter(Mandatory=$true)][string]$apiUrl,
		[Parameter(Mandatory=$true)][string]$APIKey,
		[Parameter(Mandatory=$true)][string]$RoleName,
		[Parameter(Mandatory=$true)][string]$environmentId,
		[Parameter(Mandatory=$true)][string]$OctopusBaseUrl
	)
	Write-Host "Removing existing targets in $environmentId with role $RoleName."
	$headers = @{ "X-Octopus-ApiKey" = $APIKey; }

    $machines = Invoke-RestMethod -Uri "$OctopusBaseUrl/environments/$($matchingEnvironment.Id)/Machines?take=200" -Method Get -ContentType "application/json" -Headers $headers

	Write-host "Did i get here 1"
	$machines.Items | ForEach-Object { 
		$roles = $_.Roles
		Write-Verbose "Testing existing target $($_.Name) $($_.Id) with roles: $([string]::Join(", ", $roles))"
		if ($roles -contains $RoleName) {	
         Invoke-RestMethod -Uri "$OctopusBaseUrl/Machines/$($_.Id)" -Method delete -ContentType "application/json" -Headers $headers
			Write-Highlight "Removed existing target $($_.Name)."
		}
	}
}

function Add-DeploymentTarget() {
	param(
		[Parameter(Mandatory=$true)][string]$apiUrl,
		[Parameter(Mandatory=$true)][string]$APIKey,

		# tentacle specific
		[Parameter(Mandatory=$true)][string]$name,
		[Parameter(Mandatory=$true)][string]$ipAddress,
		[Parameter(Mandatory=$true)][string[]]$roles,

		# other
		[Parameter(Mandatory=$true)][string[]]$environmentIds,
		[Parameter(Mandatory=$true)][string]$CertificateThumbprint,
		[Parameter(Mandatory=$true)][string]$OctopusBaseUrl
	)

	Write-Host "Adding new target $name $ipAddress."
	$request = @{
		Roles = $roles;
		EnvironmentIds = $environmentIds;
		Name = $name;
		Thumbprint = $CertificateThumbprint;
		Uri = (New-TentacleUri -IpAddress $ipAddress);
		IsDisabled = $false;
		Endpoint = (New-TentacleEndpoint -IpAddress $ipAddress -CertificateThumbprint $CertificateThumbprint);
	}
	Write-Verbose (ConvertTo-Json $request)
	Invoke-RestMethod -Uri "$OctopusBaseUrl/machines" -Method post -ContentType "application/json" -Body (ConvertTo-Json $request) -Headers @{ "X-Octopus-ApiKey" = $APIKey; }
	Write-Highlight "Added new target $name $ipAddress."
}

Remove-DeploymentTargets -RoleName $RoleName -EnvironmentId $($matchingEnvironment.Id) -ApiUrl $apiUrl -ApiKey $APIKey -OctopusBaseUrl $OctopusBaseUrl


$deploymentRoles = @( $RoleName, "Base Server" )

Add-DeploymentTarget -Name $ipAddress -IpAddress $ipAddress -Roles $deploymentRoles `
-EnvironmentIds $($matchingEnvironment.Id) -CertificateThumbprint $CertificateThumbprint `
-ApiUrl $apiUrl -ApiKey $APIKey -OctopusBaseUrl $OctopusBaseUrl



Step2 is just execute script on “Targets in roles” and configured as “Test”

Hi @harsh_tech,

Thank you for getting in touch.

You could look to add in a health check step for step 2 (after your new target has been created) that is attached to the role of you new Target.

Within this step, under the “Machine Selection” section you can select “Include new deployment targets in this deployment” as seen in the screenshot below:

This should include your new targets as part of the deployment.

I experienced something similar a couple of months back when playing around with dynamic targets in AWS and this helped me.

Hopefully this will help you for where you need to get to.

If this doesn’t solve your question then I can dig a little bit deeper into this for you.

All the best,
Doug

1 Like

Thanks @doug.pipe , After adding Healthcheck, it worked fine. Thank you once again

Hi @harsh_tech,

Glad it worked for you :smile:.

All the best,
Doug