Include targeted computers in email notification when deployment begins

hi-

i have an email notification as the first step in a process, so a notification is sent when a deployment begins. how can i include a list of targets in the email message, given a specific environment and role?

in other words, i’d like to be able to provide a particular role to this step, and list the targets in that role, for the current environment.

thanks!

Hi,

Thanks for getting in touch! You can write each machine that exists in a specific role using the system variable Octopus.Environment.MachinesInRole[YourRole] as shown in our system variables page.

This variable will evaluate to your appropriate machine IDs, i.e. Machines-121 Machines-141 which exist in this role. Does this variable provide the data you’re after?

Let me know how you go or if you have any further questions. :slight_smile:

Kind regards,

Kenny

thanks kenny-

i’ve tried that, but Octopus.Environment.MachinesInRole[YourRole] doesn’t seem to be working. here is the body field from the step which sends the email:

you have started a deployment:

project name: #{Octopus.Project.Name}
environment name: #{Octopus.Environment.Name}
machines: #{Octopus.Environment.MachinesInRole[infrastructure_testing]}

the email is successfully sent, but contains the following:

you have started a deployment:

project name: email_test
environment name: infrastructure-qa
machines: #{Octopus.Environment.MachinesInRole[infrastructure_testing]}

it seems that variable isn’t getting recognized. how can i troubleshoot this? we’re using 3.17.8 at the moment.

thanks!

Hi,

Thanks for following up! You can troubleshoot variables in Octopus by enabling a couple of debugging variables to your project. Specifically OctopusPrintVariables and OctopusPrintEvaluatedVariables, both with a value of True. This will then write every available variable at the beginning of each step, and what these variables then evaluate into. You can find more info in our docs. :slight_smile:

In the resulting deployment log, you should see something like this: [Octopus.Environment.MachinesInRole[role]] = 'Machines-121, Machines-141'

Your syntax looks perfect, and the variables are in fact being displayed in an email in my test on my local instance. Is the role name you’ve specified spelled correctly? Feel free to provide a copy of those verbose logs and I’ll be happy to look through them as well.

I look forward to hearing back and getting to the bottom of this!

Kind regards,

Kenny

thanks-

i’ve enabled both OctopusPrintEvaluatedVariables and OctopusPrintVariables [see screen shot]

but there don’t seem to be additional details available in the logs:

Tasks Deploy email_test release 0.0.16 to infrastructure-qa Raw log 
Task ID:        ServerTasks-52532
Task status:    Success
Task queued:    Wednesday, March 28, 2018 1:41 PM
Task started:   Wednesday, March 28, 2018 1:41 PM
Task duration:  less than a second
Server version: 3.17.8+Branch.master.Sha.aa039c216cf3aa696d04b8d66bbfcbd9c1e0b65b
Server node:    AD0OCTAPP01

                    | == Success: Deploy email_test release 0.0.16 to infrastructure-qa ==
13:41:03   Verbose  |   Guided failure is not enabled for this task
13:41:04   Info     |   The deployment completed successfully.
                    | 
                    |   == Success: Step 1: email_notification ==
13:41:03   Verbose  |     email_notification completed
                    |   
                    |     Success: Octopus Server
13:41:03   Verbose  |       Octopus Server version: 3.17.8+Branch.master.Sha.aa039c216cf3aa696d04b8d66bbfcbd9c1e0b65b
13:41:03   Verbose  |       Environment Information:
                    |       OperatingSystem: Microsoft Windows NT 6.3.9600.0
                    |       OsBitVersion: x64
                    |       Is64BitProcess: True
                    |       CurrentUser: CORP\octopus_dev
                    |       MachineName: AD0OCTAPP01
                    |       ProcessorCount: 2
                    |       CurrentDirectory: C:\Windows\system32
                    |       TempDirectory: C:\Users\octopus_dev\AppData\Local\Temp\
                    |       HostProcessName: Octopus.Server
                    |       PID: 7228

13:41:03   Verbose  |       Email parameters:
13:41:03   Verbose  |       Subject: test email
13:41:03   Verbose  |       To: user@example.com
13:41:03   Verbose  |       Cc:
13:41:03   Verbose  |       Bcc:
13:41:03   Verbose  |       Sending email message...
13:41:03   Info     |       Email sent!
13:41:03   Verbose  |       Successfully finished email_notification on the Octopus Server

the role name is spelled correctly, yes [see screen shot]:

Hi,

Thanks for following up! Did you create a new release after the addition of the debugging variables? Whenever any change is made to your variables or deployment process, a new release needs to be created to implement those changes. Alternatively, you can update this release’s variable snapshot as shown in the image below, then redeploy this release. :slight_smile:

I look forward to hearing back!

Kind regards,

thanks for the continued assistance with this-

i did create a new release, yes - see the included screen shot for release 0.0.16 showing the variables active for that release:

Hi,

Thanks for following up! I’m sorry about the delay in getting back to you. I’ve given this a bit more of a test, and I can finally see what you’re experiencing. You should be able to retrieve the values of the Octopus.Environment.MachinesInRole[infrastructure_testing] variable if you have any other step that either runs on targets in a role, or on the Octopus Server on behalf of roles. You should be able to see more details both in the email and your deployment logs if you add any dummy step that runs on or behalf of a role, as that will populate this system variable with the correct value.

I hope this helps!

Best regards,

Kenny

aha! thanks very much, that was the key. ultimately, i’d like to collect the values in Octopus.Machine.Hostname, and use those in the email step - how can i do this?

thanks!

testing a bit, i may have answered my own question. i’ve created a step [named “collect_target_hostnames”] that runs locally on the octopus server, using powershell, which does this:

Set-OctopusVariable -name “MachineHostnames” -value #{Octopus.Machine.Hostname}

that step runs first. then, i’ve adjusted the email step to do this, for the email body:

a deployment has just been initiated:

project: #{Octopus.Project.Name}
release: #{Octopus.Release.Number}
environment: #{Octopus.Environment.Name}

targets:
#{each machine in Octopus.Action[collect_target_hostnames].Output}
#{machine.MachineHostnames}#{/each}

with brief testing thus far, this seems to get me what i’m after:

a deployment has just been initiated:

project: email_test
release: 0.0.24
environment: infrastructure-qa

targets:

foo.example.com
bar.example.com

is this the right way to accomplish this?

one other question - i’ve made these two steps into step templates, as they’ll be used repeatedly among numerous projects. when that happens though, if someone decides to name their “collect_target_hostnames” step something else, this mechanism fails. how can i more elegantly handle that?

thanks!

Hi,

Thanks for following up! Your solution looks great, and it’s exactly what we’d recommend (I think it may be the only way). Since an email step is run on the Octopus server, and can’t be run on behalf of roles, these hostnames would need to be created as output variables like you’ve done so the email step can use them. Glad you got a great solution going! Don’t hesitate to reach out if you have any further questions going forward. :slight_smile:

Kind regards,

Kenny

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