Storing Nomad Variables
Nomad Variables provide the option to securely store configuration at file-like paths directly in Nomad's state store. The contents of these secrets are encrypted and replicated between servers via raft. Access to secrets is controlled by ACL policies, and tasks have implicit ACL policies that allow them to access their own secrets. You can create, read, update, or delete secrets via the command line or in the Nomad web UI.
Note that the Nomad Variables feature is intended for small pieces of configuration data needed by workloads. Because writing to the Nomad state store uses resources needed by Nomad, it's not well-suited for large or fast-changing data. For example, do not store batch job results as variables - these should be stored in an external database. Variables are also not intended to be a full replacement for HashiCorp Vault. If you need powerful options like dynamic secrets or transit encryption, continue using Vault.
For complete documentation on the Nomad Variables feature and related concepts, see the Variables reference documentation, the Key Management documentation, and the Workload Identity documentation
In this tutorial you'll store variables in Nomad via the command line.
Note
You should always protect access to variables with Access Control Lists (ACLs). Writing ACL policies for variables is covered in the Nomad Variables Access Control tutorial
Using the variables command-line interface
First, create two namespaces named prod
and dev
.
Create a variable in the prod
namespace.
Read the variable.
When writing a variable, you can use the -out
option to display the output.
The var put
and var get
commands both accept the -out
option. You can read
variables in a machine-friendly format using the -out json
option:
Updating variables
You can use var put
to overwrite all items in a variable, but to avoid
conflicting with other writes that may have happened since you last read the
variable, you must use the -check-index
flag and set it to the last modified
index.
Note
Running the following command without updating the check-index to the ModifyIndex value printed by the previous command will result in a Check-and-Set conflict.
Try it again, this time leaving off the -check-index
flag (or setting it to an
index value in the future). Note the value is not updated and you receive the
current value to avoid conflicts.
To patch a variable without overwriting all the values, you can pipe var get
into var put
with the initial value set to -
to signal that you're accepting
input from stdin
. This will automatically set the -check-index
flag so that
you avoid conflicts.
The command will output the updated variable in JSON format.
List the two variables. Note that the list response only includes the paths and metadata, not any of the data.
List the variables, using a prefix filter. Note that only variables at the path matching the prefix are shown.
Create three more variables, this time in the dev
namespace.
Using the wildcard namespace indicator (*
), list all the variables you have
access to. For many shells, the *
character is significant, so you might need
to wrap it in double ("
) or single ('
) quotation marks.
You can list the variables in a given namespace by using the namespace
option. For example, list all of the variables in the dev
namespace by adding
-namespace dev
to the nomad var list
command.
Next steps
Now that you have used the nomad var
command to create and view Nomad
Variables, consider learning more about how to control access to Nomad Variables
with the Nomad Variables Access Control tutorial and how to use Nomad
Variables in your jobs with the Accessing Variables From Tasks tutorial.