Getting variable using another variable

Hi,
I’d like to get user password, using other variable - service name.

Example:
#{Name} = “SomeName”
#{SomeName_password} = “SomePassword”

And i want to get #{SomeName_password} , but only specifying in project #{Name} variable, and getting password by convention. But I cannot do something like this: #{#{Name}_password}

As workaround I use password variables with key: User[SomeName].Password, and using in template
#{each user in User}#{if user == Name}#{user.Password}#{/if}#{/each}

Is there maybe a better way?

Hi,

Thanks for getting in touch. You’re correct that it’s not possible to substitute a variable within a variable however we do support substitution within an index. For example, if you had variables like the following:

  • MyPassword[Rob] = passwordx
  • MyPassword[Steve] = passwordy
  • MyPassword[Mary] = passwordz

And if you had a variable #{SomeName} = Rob then you could do the following and it will work fine.
#{MyPassword[#{SomeName}]}

This works because we added support to perform variable substitution inside an index as of Octopus 3.3.23 and newer.

The other option is if you defined your variables in a JSON string like the following:
MyPassword = {"Rob": "passwordx", "Steve": "passwordy", "Mary" : "passwordz"} then you do the following and it will work fine as well.
#{MyPassword[#{SomeName}]}

This is available in Octopus 3.5 and newer.

Hope this helps!

Rob

The first option is what I need, thanks. It would by nice to add this to documentation. I didn’t even think, that if substitution does not work within variable, it will work with index.

The second option will be troublesome, because of password encryption.

Thanks again.

Hi,

Thanks for following-up. I agree that it’s not immediately clear that variable substitution within indexes works. I updated our variable substitution syntax page so this is clearly documented and more easily searchable. Previously, it was only mentioned in a github issue and a blog post.

Thanks again!

Rob