Accessing output variables using python

HI Team,

i have created this output variable iso_time in step1 , and i want to use this variable in setp 2.
step1:
import datetime
iso_time = datetime.datetime.now().isoformat()
set_octopusvariable(“isotime”, “iso_time”)

step2 :

get_octopusvariable(“Octopus.Action[step2].Output.iso_time”)

i have tried this, but doesn’t seems to be working.

Hi @chandramouli429,

Thanks for getting in touch!

When you’re retrieving the variable in step 2, you should be referencing the step 1 name.
e.g.
get_octopusvariable(“Octopus.Action[step1].Output.iso_time”)
instead of
get_octopusvariable(“Octopus.Action[step2].Output.iso_time”)

Hope this helps.

Regards,
Paul

HI @paul.calvert

Thanks for the quick response.

I have updated it. i’m facing the below error

get_octopusvariable(“Octopus.Action[step1].Output.iso_time”)

File “C:\Octopus\Work\20200815125111-525303-9418\Configure.c3d-435f-8d2f-c4c7af7e5229.py”, line 24, in get_octopusvariable
return octopusvariables[key]

KeyError: ‘Octopus.Action[Step1].Output.iso_time’

Fatal

The remote script failed with exit code 1

Hi Chandramouli429,

Paul is right here, but I’ll provide an example so you can see exactly how it works. So lets name Step 1: “DateTimeStep”. This is important for step 2.

So within Step 1, you would have the following:

import datetime
iso_time = datetime.datetime.now().isoformat()
set_octopusvariable("isotime", iso_time)

As you can see, this is very similar to the original step you posted. The only difference is setting the variable within the last argument within set_octopusvariable. This shouldn’t be in quote marks.

Step 2.

iso_time="#{Octopus.Action[DateTimeStep].Output.isotime}"
print (iso_time)

So within Step 2, the iso_time variable is in quote marks because after variable substitution it will be a string. Variable substitution solves the rest. #{Octopus.Action[DateTimeStep].Output.isotime} gets replaced with the current date time.

You can get more information on these two variable at this link.

Have a great day and let me know if I can help further.

Regards,

Dane.

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