Docs
Concepts

Variables

How a design takes input, passes values to the designs it uses, and computes values.

Variables make one design build many things. The compute rack builds any rack in any site, because the rack's name and its site are variables.

Declaring them

A design declares its variables at the top:

variables:
  rack_name: $USER
  site_slug: $USER
  status: active
ValueMeans
$USERYou must give a value when you plan. The deploy page asks for it
$USER_OPTIONALYou may give one. Left out, every field that uses it is left out
A value, such as activeA default. The deploy page doesn't ask for it
An expression, such as '{{ variables.site_name | slugify }}'Computed from other variables

Using them

Anywhere a value can go, an expression in {{ }} can too:

  • {{ variables.rack_name }} is a variable's value. The rack is named '{{ variables.rack_name }}', and its switch '{{ variables.rack_name }}-TOR-01'.
  • {{ parent }} is the object that contains this one, and {{ parent.site }} a field of it.
  • {{ loop.i }} is the current pass of an nbd_loop, so the servers are numbered '{{ variables.rack_name }}-COMPUTE-{{ loop.i | zeropad }}': R1-COMPUTE-01 to R1-COMPUTE-10.
  • Filters change a value: zeropad pads a number to two digits, and lower, upper and slugify change text.
  • Arithmetic works on numbers: each server's second data port connects to switch port {{ loop.i * 2 }}.

Passing them to the designs you use

A design included with nbd: has variables of its own. Its template_vars sets them:

- design: nbd:devices/Dell_R640_w_module
  template_vars:
    name: '{{ variables.rack_name }}-COMPUTE-{{ loop.i | zeropad }}'
    position: '{{ loop.i }}'
    site: '{{ parent.site }}'
    rack: '{{ parent }}'

Each value is worked out in the including design, where variables, parent and loop mean the rack's. The server design then sees them as its own variables.name, variables.position and so on. The server design leaves serial as $USER_OPTIONAL, and the rack doesn't pass one, so no server has a serial.

Typed variables at general availability

Today every variable is text until a field uses it. At general availability, a design can declare what each variable is, such as a number, an IP prefix or one of a list of choices. Designs can then check what you enter before it plans, and the deploy page can ask for each in the right form.

Variables reference covers computed defaults, optional variables and the rules for passing values down.

On this page