Add Tentacle Roles via script

Hi,
I’m trying to deploy tentacles (and determine which roles to apply) automatically, during the creation of a VM.
I am successfully using the OctopusDSC resource to install & register the tentacle - which I’m happy with.
Unfortunately this will not dynamically assign roles on the fly (as each VM may have different set of roles) - so whatever role is set during MOF creation is the only one that gets applied.
I already have a github issue open for this https://github.com/OctopusDeploy/OctopusDSC/issues/33.

In the absence of this being resolved…
Could I run a powershell script afterwards, to determine the Roles I need, and add them to the (already set up) tentacle.
Can I use the OctopusAPI for this? Or Tentacle.exe?

I looked into invoking the “api/machines” GET method - which returns a full list of machines, including their roles.
Ideally I could then pull out Roles of a previous VM, copy them, and add them to the new created VM.
Is this even possible?
I’m having trouble pulling out the Roles item I need, never mind adding them to the required VM.

Alternatively, could I use the Tentacle.exe to add on roles?? (i.e. AFTER the tentacle is registered by the DSC resource).

Many thanks for any help you can provide!
Bronwyn,

Hi Bronwyn,

Thanks for getting in touch.

As I mentioned on the github ticket, I’m not convinced the PR to load environments and roles from a file is the correct way to do this. I’d suggest looking at how to separate configuration and environment data.

You can definitely modify the roles after the DSC is run. You could use Tentacle.exe to re-register the roles/environments (as is done in the DSC), or you could use OctoPosh as per this example. Octopus is completely API driven, so anything you can do with the UI is possible with the API.

Hope that helps.

Matt

Thanks so much Matt!

I was not aware of this (amazing!) OctoPosh module and its exactly what I needed to sort out my Roles etc! Brilliant!

One thing that would be great, if it was able to modify the Tentacle DisplayName also? That doesnt seem to work?

Ideally, each of our Octopus tentacles’ DisplayName would be the Machine name, so during the cTentacleAgent config, I’ve left it at the default <MachineName>_<InstanceName>, with the being Tentacle.
Any way I could lose the _<InstanceName> part? leaving the name as the MachineName?

I cant do this during the DSC config/MOF creation, as I want it to take the name of each DSC client it runs on.

Thanks!
Bronwyn.

Hi Bronwyn

Can you give me a bit more information on why it doesn’t work? The PR to support DisplayName was merged just last week, and the tests seem to confirm it works?

If you do not supply the property, it will default to <machinename>_<name> (where name is the Tentacle instance name. It needs to do this as its reasonably common to have multiple Tentacle’s installed on the same machine.

I suspect that the root of this issue is trying to use the same mof file across different machines. DSC really works better if you’ve got a per machine mof file.

But, if thats not possible, you could again use OctoPosh to modify the machine display name at the same time as you modify the roles / environments.

Hope that helps!

Regards,
Matt

Thanks Matt,

I hadn’t realised the new PR had been applied to include DisplayName in cTentacleAgent, but supplying the name here does not help me as (like you say) I’m using the same mof file for more than one machine, so I only know the machine name I want at runtime. (I typically only have one Tentacle per machine).

However, I’m happy to use OctoPosh for this, at the same time as I’m modifying the roles - but I cannot find the right syntax?

For the roles I am doing:

$Tentacle = Get-OctopusMachine -MachineName “MyMachineName”

$Tentacle.Resource.Roles.Add(“MyRole”)

Update-OctopusResource -Resource $Tentacle.Resource -Force

If I try to add the Name property before calling Update-OctopusResource, it does not apply:

i.e.
$Tentacle.Resource.Roles.Add(“MyRole”)

$Tentacle.Name = “NewDisplayName”

Update-OctopusResource -Resource $Tentacle.Resource -Force -> does not include the Name change

or

Update-OctopusResource -Resource $Tentacle.Name -Force -> this gives an error about a invalid object type.

I can’t find enough documentation or examples for updating the DisplayName after the tentacle has been created.

Many thanks!
Bronwyn.

Hi Bronwyn,

All the changes you want to make to the Resource on the database (in this case a Machine resource), need to be made to $Tentacle.Resource and not just to $Tentacle. At the same time, when you send a resource to be updated to Update-OctopusResource, you need to pass the object $Tentacle.Resource and not just $Tentacle.

In your script you are changing the name of $Tentacle.Name, when you should be changing $Tentacle.Resource.Name. So it should be like this:

$Tentacle.Resource.Roles.Add("MyRole")

$Tentacle.Resource.Name = "NewDisplayName"

Update-OctopusResource -Resource $Tentacle.Resource -Force

Let me know if that works,
Dalmiro

Thankyou so much Dalmiro!

I cant believe i didnt try that variation - makes sense now you look at it, and works perfectly!

Thanks all for your help - and thanks so much for Octoposh! Its helping me so much!

:slight_smile: