Skip to content

Variables

Jason Mueller edited this page Mar 18, 2016 · 8 revisions

Configuration variables provide a way to vary the configuration of your application at deployment time.

Providing Variables

Install-DeploymentPackage and Invoke-Powerdeploy each have a parameter to provide configuration variables to an application being deployed. The Variable parameter takes a hash table of name value pairs representing the variables for the deployment.

You provide the variables at deployment time by passing them in when installing the package locally:

Install-DeploymentPackage `
        -PackageArchive SamplePackage_1.2.3.zip `
        -Environment Integration `
        -Variable @{ 'debuglevel' = 'ERROR'; 'databaseinstance' = 'integration' }

or invoking a remote deployment:

Invoke-Powerdeploy `
        -PackageArchive SamplePackage_1.2.3.zip `
        -Environment Integration `
        -ComputerName webserver01
        -Variable @{ 'debuglevel' = 'ERROR'; 'databaseinstance' = 'integration' }

Powerdeploy will apply these variables automatically in certain situations and you can also use them explicitly.

Managing Configuration Variables

Supplying configuration variables as a parameter at deployment time offers great flexibility for varying configuration and testing deployments, but it doesn't promote repeatability of deployments or visibility of the configuration in effect at a point in time.

Ideally, configuration variables would be versioned, like source code, and auditable.

Powerdeploy does not force the use of any specific configuration management solution and is perfectly content to take the variables hash table from any source. You could (and should) wrap Powerdeploy in a script that pulls variables from a repository for a given application version targeted at a specific environment and then pass those in on the command line. The repository could be a SQL database, a web service, a filesystem, or any other conceivable source that could return name-value pairs based on some deployment criteria.

This approach is common with Powerdeploy, so an example wrapper script is included with Powerdeploy that can be used as-is or tweaked as needed. DeployPackage.ps1 (in the Examples folder) is a simple script which takes many of the parameters required by Invoke-Powerdeploy. It retrieves configuration variables and invokes Powerdeploy with the variables it gets back.

Although you can roll your own, Powerdeploy also includes support for a simple filesystem based configuration repository.

Reviewing Configuration Variables

Once your configuration is made available, you can use Get-ConfigurationVariable to retrieve the applicable configuration.

C:\> Get-ConfigurationVariable `
    -SettingsPath c:\mysettings.store `
    -ApplicationName MyWebsite `
    -Version 1.0.0 `
    -EnvironmentName Integration `
    -ComputerName testserver | Format-Table
Scope                               Name              Value                                                                      Type      ScopeName                      
-----                               ----              -----                                                                      ----      ---------                      
{Environment}                       SqlDatabase       Data Source=DBServer\int;Initial Catalog=AppData;Integrated Security=True; NameValue {integration}                  
{Environment}                       PaymentServiceUrl http://paymentsite.local/test                                              NameValue {integration}                  
{Application, Version}              HostUrl           http://localhost:8888                                                      NameValue {MyWebsite, 1.0.0}             
{Application, Version}              ApiVersion        1.0.0                                                                      NameValue {MyWebsite, 1.0.0}             
{Application, Version}              PaymentProcessor  ${env:PaymentServiceUrl}                                                   NameValue {MyWebsite, 1.0.0}             
{Application, Version}              OtherServiceUrl   http://otherhost:8122/getvalues                                            NameValue {MyWebsite, 1.0.0}             
{Application, Version}              DebugLevel        Verbose                                                                    NameValue {MyWebsite, 1.0.0}             
{Application, Version, Environment} DebugLevel        Warn                                                                       NameValue {MyWebsite, 1.0.0, integration}

A collection of objects will be returned that indicate the name and value of the variable as well as the scope of the variable.

You can filter the variables using the appropriate switches. If only SettingsPath is supplied, all variables will be returned.

See examples for filesystem based configuration for examples of how the filtering works.

Previewing variable replacement

Since environment-scoped variables aren't applicable unless referenced by application-scoped variables, you need to merge them together before to see what he actual values will be. The -Resolve parameter instructs Get-ConfigurationVariable to merge environment-scoped variables into any application-scoped variables that reference them.

C:\> Get-ConfigurationVariable `
    -SettingsPath c:\mysettings.store `
    -ApplicationName MyWebsite `
    -Version 1.0.0 `
    -EnvironmentName Integration `
    -ComputerName testserver `
    -Resolve | Format-Table
Scope                               Name             Value                           Type      ScopeName                      
-----                               ----             -----                           ----      ---------                      
{Application, Version, Environment} DebugLevel       Warn                            NameValue {MyWebsite, 1.0.0, integration}
{Application, Version, Environment} HostUrl          http://localhost:8888           NameValue {MyWebsite, 1.0.0, integration}
{Application, Version, Environment} ApiVersion       1.0.0                           NameValue {MyWebsite, 1.0.0, integration}
{Application, Version, Environment} PaymentProcessor http://paymentsite.local/test   NameValue {MyWebsite, 1.0.0, integration}
{Application, Version, Environment} OtherServiceUrl  http://otherhost:8122/getvalues NameValue {MyWebsite, 1.0.0, integration} 

In the output using -Resolve, notice that only application values are returned. The environment-scoped variables are no longer required because we've resolved all of the references to environment-scoped variables that were used in the application-scoped variables. Since Powerdeploy does not use environment-scoped variables unless they are explicitly referenced, they are not needed once they've been resolved.

Also notice that the PaymentProcessor variable value is now the actual URL and not an environment placeholder.

Generating a hashtable

Powerdeploy expects a hashtable of variables to be supplied on the command line. Get-ConfigurationVariable will provide a hashtable of variables if the -AsHashTable parameter is specified. The -Resolve parameter is required in order to use -AsHashTable. Using -Resolve and -AsHashTable provide the ability to transform environment and application configuration files to the format required for deployment.

Get-ConfigurationVariable `
    -SettingsPath c:\mysettings.store `
    -ApplicationName MyWebsite `
    -Version 1.0.0 `
    -EnvironmentName Integration `
    -ComputerName testserver `
    -Resolve `
    -AsHashTable
Name                           Value                          
----                           -----                          
HostUrl                        http://localhost:8888          
ApiVersion                     1.0.0                          
PaymentProcessor               http://paymentsite.local/test  
OtherServiceUrl                http://otherhost:8122/getvalues
DebugLevel                     Warn   

When the variables are output as a hashtable, all of the extraneous properties of the variables is excluded. Only the name value pairs are returned. This is the format that Powerdeploy expects to be specified when using Invoke-Powerdeploy or Install-DeploymentPackage.

Questions

Why not just embed the settings in the package?

CD pg. 41-42

Passed in vs. retrieved

Storage

Scope & Precedence

Clone this wiki locally