Skip to content

Commit

Permalink
Merge pull request #2488 from hashicorp/f-node-values-template
Browse files Browse the repository at this point in the history
Access Node Meta and Attrs in template
  • Loading branch information
dadgar committed Mar 28, 2017
2 parents b7cf08b + d156219 commit 3750f97
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/consul_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ func (t *TaskEnvironment) EnvMap() map[string]string {
return m
}

// 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))
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
Expand Down
25 changes: 22 additions & 3 deletions website/source/docs/job-specification/template.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = <<EOH
---
node_dc: {{ env "node.datacenter" }}
node_cores: {{ env "attr.cpu.numcores" }}
meta_key: {{ env "meta.node_meta_key" }}
EOH
destination = "local/file.yml"
}
```

## Client Configuration

The `template` block has the following [client configuration
options](/docs/agent/config.html#options):
Expand All @@ -148,3 +166,4 @@ options](/docs/agent/config.html#options):
[ct]: https://github.com/hashicorp/consul-template "Consul Template by HashiCorp"
[artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
[env]: /docs/runtime/environment.html "Nomad Runtime Environment"
[nodevars]: /docs/runtime/interpolation.html#interpreted_node_vars "Nomad Node Variables"

0 comments on commit 3750f97

Please sign in to comment.