Variables and Presets
The Key to Reusable Infrastructure Definitions
Introduction
To enable a blueprint to be customized and reused in different environments, we introduce the concepts of variables and presets. Variables allow different environments to consume the same blueprint but behave differently. If the critical design choices in a blueprint are made into variables, the blueprint will become more reusable for a wider audience. When a blueprint is added to an environment, the values of the variables must be provided before the environment can be applied.
Variables
If you wanted to create a database blueprint that is used in production, staging, and development environments, that blueprint would likely describe disks for data storage. The disk size and type for a production environment may be quite different from that of a development environment. If we take the example from the last section and enhance it, we can make the size of the disk a variable:
variables:
disk_type:
description: The type of disk to use for the record and log disks.
type: string
log_disk_size:
description: The size of the database log disk.
type: string
default: 64 gb
records_disk_size:
description: The size of the database records disk.
type: string
default: 256 gb
storage:
disk:
records:
size: (( records_disk_size ))
type: (( disk_type ))
logs:
size: (( log_disk_size ))
type: (( disk_type ))
In this example, variables replace all of the static properties from the previous example. The variable disk_type
has no default value and therefore must be set to a value before an environment containing this blueprint can be applied.
Count Variable
When a resource is counted, an automatic variable named count
is created.
Mathematical Expressions
Integer variables can be used in simple mathematical expressions, for example you may choose to use the count variable in a counted resource to spread those resources across availability zones:
storage:
disk:
records:
count: 3
size: 2 TiB
type: hdd
zone: (( ((count - 1) % 3) + 1 ))
When the blueprint is processed, disks will appear in different availability zones:
Disk Name
Zone
records
((1 - 1) % 3) + 1
or 1
records-2
((2 - 1) % 3) + 1
or 2
records-3
((3 - 1) % 3) + 1
or 3
Naming by Variable
You can use variables in the blueprint name of an object, either partially or completely:
variables:
pod:
description: The root name of the pod this belongs to.
type: string
regex: "a-z"
location:
folder:
(( pod ))-internal-(( count )):
region: preferred
count: 2
(( pod ))-external:
region: preferred
region:
preferred:
aws: us-west-2
azure: westus2
Padding Numbers
Padding allows you to add leading zeros to an integer variable as part of a name like when using our count
variable. You can do this by specifying you want to pad
the variable and what the length of the integer should be. For example, to ensure two digit numbers in the example above you can specify pad
and then the specify the length of the integer,
(( pad(count, 2) ))
will ensure a single digit count renders as 01
, 02
, 03
, etc.
(( pod ))-internal-(( pad(count, 2) ))
Value Limiters
Integers
You can specify min
and max
range checks for variables.
Strings
You can specify a regex
(Regular Expression) that the variable value must adhere to.
Presets
Presets are collections of predetermined variable settings that embody best practices configurations. By providing presets, the blueprint author can make it easier for provisioners to add blueprints to their environments. If the author of the example database blueprint wants to provide presets for the variable definitions above, they may look like this (noting they would need to mark the variables as presets):
variables:
disk_type:
description: The type of disk to use for the record and log disks.
type: string
preset: True
log_disk_size:
description: The size of the database log disk.
type: string
default: 64 gb
preset: True
records_disk_size:
description: The size of the database records disk.
type: string
default: 256 gb
preset: True
presets:
deployment_type:
production:
disk_type: ssd
log_disk_size: 64gb
records_disk_size: 2 TiB
development:
disk_type: hdd
log_disk_size: 16gb
records_disk_size: 128 GiB
Once a variable is marked as preset, it may not be set independently, although it may have a default value. Preset variables and non-preset variables can be mixed and more than one preset may be used, although a variable cannot be set in multiple presets.
This technique shows how a single blueprint can drive reuse in different use cases. Businesses can empower developers to deploy the same infrastructure as the production site, but at a reduced scale (and cost). A production environment and a development environment can attach the same blueprint and achieve different configurations controlled by variables.
Presets As a Variable
Presets are also available as variables and can be used throughout the blueprint, for example:
location:
folder:
(( deployment_type ))-internal:
region: preferred
region:
preferred:
aws: us-west-2
azure: westus2
Popular Variables
The following decision points are often made into variables to make blueprints more reusable:
The region to deploy into.
The folder resource name to use.
The number of virtual machines to create.
The size and type of disks.
Last updated
Was this helpful?