Variable variable
The variable
resource allows you to create modular and reusable configuration.
Variables are defined using variable resouce, they allow the author of a blueprint
to provide default values which can be overriden my the following methods:
- Variable definition files
- Flags provided to Jumppad up
- Environment variables
Any variable defined is local to the current module.
Properties
- Name
value
- Type
- (string: "")
- Required
- required
- Readonly
- Description
The value of the variable.
Note: Variables are processed before resources and can not contain any interpolated values.
Examples
Simple Variables
The following eample defines two variables version which has a value 1.6.1,
and subnet which has a value of 10.6.0.0/16. To use these variables inside
the configuration you use the variable.[variable_name]
syntax. When using a variable
on its own it is not required to encapsulate this in a string, as can be seen
in the subnet example subnet = var.subnet, however; should you need to concatonate
this variable with another then you need to encapsulate the variable.[variable_name]
inside the parentheses ${}
, this is seen in the container image stanza,
name = "consul:${variable.version}"
.
variable "version" {
default = "1.6.1"
}
variable "subnet`" {
default = "10.6.0.0/16"
}
resource "network" "onprem" {
subnet = variable.subnet
}
resource "container" "consul" {
image {
name = "consul:${variable.version}"
}
command = ["consul", "agent", "-dev"]
network {
id = resource.network.onprem.meta.id
ip_address = "10.6.0.200"
}
}
Complex Example
In addition to specifying simple string variables, it is also possible to define variables which are maps, or arrays. The following example shows the use of both of these types. In addition to the example shown below, a map can also contain an array and an array a map allowing you to mix complex types together.
variable "subnet" {
default = {
main = "10.6.0.0/16"
consul = "10.7.0.0/16"
}
}
variable "command" {
default = [
"consul",
"agent",
"-dev"
]
}
resource "network" "onprem" {
subnet = variable.subnet.main
}
resource "container" "consul" {
command = variable.command
}
Overriding Variables
The variable
resource allows the specification of a default value for a variable,
overriding these variables can be performed using the three following methods:
Variable Files
When reading a configuration folder Jumppad will auotmatically search for and parse files with the extension .vars. Variable files allow you to set the value for complex and simple variables and are specified as seen in the following example:
version = "1.8.1"
subnet = {
main = "192.1.0.0/16"
consul = "192.2.0.0/16"
}
In addition to variable files being automatically loaded by Jumppad from the
module folder you can specify external files using the command line flag --vars-file
.
There is no naming convention for variable files specified in this way.
jumppad up --vars-file="./myvariables.defaults" ./module
Command Line Arguments
It is possible to override variables using command line arguments, the run and test commands have the flag --var which has a value variable=value pair that can be used to set a varaible. The --var flag can be specified multiple times.
jumppad up --var="version=1.9.1" --var="another=value" ./module
Environment Variables
Lastly you can specify variables using environment variables. To define a variable
using an environment variable you prefix the name of the variable with HCL_VAR_
,
for example, the variable version when set as an environment variable would
be specified as HCL_VAR_version
.
export SY_VAR_version=1.8.2
Variable Load order
When using variables there is a defined order of precidence. The following list shows the priority order for setting varaibles.
- variable stanza block
[name].vars
files found in the config folder- environment variables
HCL_VAR_[name]
- command line argumens specified with the
--var
flag - variable files specified using the
--var-file
command line flag