How to retreive Machine Name from other Deployment Target (tentacle)?

Hi,
I am aware of the system variable which retrieves the HostName of the current deployment target.
Octopus.Machine.Hostname
Bu is it possible if I have 2 machines - one with role AppServer and second with role PerformanceServer, once my step is executed on AppServer for Tenant1 and Environment1 to retrieve the hostname of the target with the role PerformanceServer for that same Tenant1 and for the Same Environment1 since it will always be just one server for the comibnation of one role, one environment and one tenant.

I know this can be achieved with API calls probably, but is there something already that can be used to retrieve those values?
Thanks!

Hi @veljkosbbb,

Thanks for the question!

We have a deployment system variable which might get you some of the way. It’s called Octopus.Environment.MachinesInRole[\_role\_] which lists the machines in a specified role being deployed to.

An example of the variable’s value is shown below:

Octopus.Environment.MachinesInRole[shared] = 'Machines-162, Machines-141'

In the above case, 2 machines share the role named: shared. You can replace the word shared with your own role name.

Unfortunately, the values are only the machine-ids and not the hostname. One way to get the hostname would be to take the value for your PerformanceServer role and then query the machine API endpoint.

I hope that helps :slight_smile:

Happy Deployments!
Mark

Hi @mark.harrison,

thanks for quick response.
I noticed this variable - but I do not think it fits my needs since it does not filter by Tenant doesn’t it? I need it for specific Tenant. I think I will have to go with one Octopus API call in order to retrieve this - I managed to do it through the API.

And can you please give me an example of the API query that I should run on the machine API? I am not sure on which API exactly are you referring to? Octopus API? Please if you can clarify briefly.

Thanks!

1 Like

Hi @veljkosbbb,

I noticed this variable - but I do not think it fits my needs since it does not filter by Tenant doesn’t it?

The variable doesn’t filter by the tenant in that it doesn’t contain machines for all tenants. Instead, the deployment should only have machines in the roles for the tenant being deployed to (A deployment task deploys to at most a single-tenant, further tenants will have their own respective deployment/task id)

can you please give me an example of the API query that I should run on the machine API?

Sorry if I wasn’t clearer in my previous answer, I meant the Octopus REST API, and specifically the machine endpoint.

Using the machine id of Machines-162 as an example, you can query the endpoint (for each machine which has the role you are looking at) using PowerShell like this:

# Replace these values
$OctopusAPIKey="API-KEY-HERE" #Octopus API Key
$OctopusURL = "https://your-octopus-server-url" #Octopus URL
$SpaceID = "Spaces-1" # The Space for the machine, usually Spaces-1 is the default space.
$MachineId = "Machines-162"

$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }

$machine = (Invoke-WebRequest $OctopusURL/api/$SpaceId/machines/$MachineId -Method Get -Headers $header -UseBasicParsing).Content | ConvertFrom-Json

# Print out machine details
$machine

This will return you details of the hostname:

I hope that helps!

Best regards
Mark

1 Like

Thanks this clarified everything and it works :slight_smile: API works great!

1 Like

Hi @veljkosbbb,

That’s great to hear!

Happy Deployments!
Mark

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.