From 63978511be1f540202ba736ec5cd3fd8285aabee Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 27 Mar 2017 14:58:04 -0700 Subject: [PATCH 1/2] Access Node Meta and Attrs in template This PR allows accessing the Node's attributes and metadata as in a template. ``` template { data = "{{ env \"attr.unique.network.ip-address\" }}" destination = "local/out" } ``` --- client/consul_template.go | 2 +- client/driver/env/env.go | 14 +++++++++++ .../docs/job-specification/template.html.md | 25 ++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/client/consul_template.go b/client/consul_template.go index 1a656a3ea656..e3f534f7eefd 100644 --- a/client/consul_template.go +++ b/client/consul_template.go @@ -350,7 +350,7 @@ func templateRunner(tmpls []*structs.Template, config *config.Config, } // Set Nomad's environment variables - runner.Env = taskEnv.Build().EnvMap() + runner.Env = taskEnv.Build().EnvMapAll() // Build the lookup idMap := runner.TemplateConfigMapping() diff --git a/client/driver/env/env.go b/client/driver/env/env.go index c2aacd38332d..1640d8e6ee8c 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -287,6 +287,20 @@ func (t *TaskEnvironment) EnvMap() map[string]string { return m } +// EnvMap returns the environment variables that will be set as well as node +// meta/attrs in the map. This is appropriate for interpolation. +func (t *TaskEnvironment) EnvMapAll() map[string]string { + m := make(map[string]string, len(t.TaskEnv)) + for k, v := range t.TaskEnv { + m[k] = v + } + for k, v := range t.NodeValues { + m[k] = v + } + + return m +} + // Builder methods to build the TaskEnvironment func (t *TaskEnvironment) SetAllocDir(dir string) *TaskEnvironment { t.AllocDir = dir diff --git a/website/source/docs/job-specification/template.html.md b/website/source/docs/job-specification/template.html.md index 88fa109c70ec..0c3588a49159 100644 --- a/website/source/docs/job-specification/template.html.md +++ b/website/source/docs/job-specification/template.html.md @@ -41,8 +41,9 @@ job "docs" { ``` Nomad utilizes a tool called [Consul Template][ct]. Since Nomad v0.5.3, the -template can reference [Nomad's runtime environment variables][env]. For a full -list of the API template functions, please refer to the [Consul Template +template can reference [Nomad's runtime environment variables][env]. Since Nomad +v0.5.6, the template can reference [Node attributes and metadata][nodevars]. For +a full list of the API template functions, please refer to the [Consul Template README][ct]. ## `template` Parameters @@ -137,7 +138,24 @@ template { } ``` -### Client Configuration +### Node Variables + +As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata. + +```hcl +template { + data = < Date: Tue, 28 Mar 2017 11:10:11 -0700 Subject: [PATCH 2/2] feedback --- client/driver/env/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 1640d8e6ee8c..c96a724f43d4 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -287,7 +287,7 @@ func (t *TaskEnvironment) EnvMap() map[string]string { return m } -// EnvMap returns the environment variables that will be set as well as node +// EnvMapAll returns the environment variables that will be set as well as node // meta/attrs in the map. This is appropriate for interpolation. func (t *TaskEnvironment) EnvMapAll() map[string]string { m := make(map[string]string, len(t.TaskEnv))