You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `variables.hcl` file defines the variables required to fully render and deploy all the templates found within the "templates" directory.
74
75
75
-
These variables are defined using [HCL](https://github.com/hashicorp/hcl).
76
+
These variables are defined using [HCL][].
76
77
77
78
An example `variables.hcl` file:
78
79
79
-
```
80
+
```hcl
80
81
variable "datacenters" {
81
82
description = "A list of datacenters in the region which are eligible for task placement."
82
83
type = list(string)
@@ -114,7 +115,7 @@ The `outputs.tpl` is an optional file that defines an output to be printed when
114
115
115
116
Output files have access to pack variables and template helper functions. A simple example:
116
117
117
-
```
118
+
```go
118
119
Congrats on deploying [[ .nomad_pack.pack.name ]].
119
120
120
121
There are [[ .hello_world.app_count ]] instances of your job now running on Nomad.
@@ -138,9 +139,9 @@ Templates are written using [Go Template Syntax](https://learn.hashicorp.com/tut
138
139
139
140
Unlike default Go Template syntax, Nomad Pack uses "[[" and "]]" as delimiters.
140
141
141
-
An example template using variables values from above:
142
+
The following is an example template using variables values from earlier.
142
143
143
-
```
144
+
```hcl
144
145
job "hello_world" {
145
146
region = "[[ .hello_world.region ]]"
146
147
datacenters = [ [[ range $idx, $dc := .hello_world.datacenters ]][[if $idx]],[[end]][[ $dc | quote ]][[ end ]] ]
@@ -176,11 +177,12 @@ job "hello_world" {
176
177
177
178
This is a relatively simple template that mostly sets variables.
178
179
179
-
The `datacenters` value shows slightly more complex Go Template, which allows for [control structures](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#control-structure-list) like `range` and [pipelines](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#pipelines).
180
+
The `datacenters` value demonstrates slightly more complex Go Templating, which
181
+
allows for [control structures](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#control-structure-list) (like `range`) and [pipelines](https://learn.hashicorp.com/tutorials/nomad/go-template-syntax#pipelines).
180
182
181
183
#### Template Functions
182
184
183
-
To supplement the standard Go Template set of template functions, the (masterminds/sprig)[https://github.com/Masterminds/sprig] library is used. This adds helpers for various use cases such as string manipulation, cryptographics, and data conversion (for instance to and from JSON).
185
+
To supplement the standard Go Template set of template functions, the [Masterminds/sprig](https://github.com/Masterminds/sprig) library is used. This adds helpers for various use cases such as string manipulation, cryptography, and data conversion (for instance to and from JSON).
184
186
185
187
Custom Nomad-specific and debugging functions are also provided:
186
188
@@ -193,7 +195,7 @@ Custom Nomad-specific and debugging functions are also provided:
193
195
194
196
A custom function within a template is called like any other:
195
197
196
-
```
198
+
```go
197
199
[[ nomadRegions ]]
198
200
[[ nomadRegions | spewDump ]]
199
201
```
@@ -202,22 +204,25 @@ A custom function within a template is called like any other:
202
204
203
205
For complex packs, authors may want to reuse template snippets across multiple resources.
204
206
205
-
For instance, if we had two jobs defined in a pack, and we knew both would re-use the same `region`
206
-
logic for both, we could use a helper template to consolidate logic.
207
+
For instance, suppose you have two jobs defined in your pack and both jobs reuse
208
+
the same `region` logic. You can create a helper template to centralize that
209
+
logic.
207
210
208
-
Helper template names are prepended with an underscore "\_" and end in ".tpl". So we could define a helper called "\_region.tpl":
211
+
Helper template names are prepended with an underscore `_` and end in `.tpl`.
212
+
You could define a helper called "\_region.tpl" with the following code.
209
213
210
-
```
214
+
```hcl
211
215
[[- define "region" -]]
212
216
[[- if not (eq .hello_world.region "") -]]
213
217
region = [[ .hello_world.region | quote]]
214
218
[[- end -]]
215
219
[[- end -]]
216
220
```
217
221
218
-
Then in the parent templates, "job_a.nomad.tpl" and "job_b.nomad.tpl", we would render to the helper template using its name:
222
+
In the parent templates, "job_a.nomad.tpl" and "job_b.nomad.tpl", you use
223
+
the template's name to render it in place.
219
224
220
-
```
225
+
```hcl
221
226
job "job_a" {
222
227
type = "service"
223
228
@@ -231,9 +236,9 @@ job "job_a" {
231
236
232
237
Packs can depend on content from other packs.
233
238
234
-
First, packs must define their dependencies in `metadata.hcl`. A pack block with a dependency would look like the following:
239
+
First, packs must define their dependencies in `metadata.hcl`. A pack block with a dependency would look like the following.
235
240
236
-
```
241
+
```hcl
237
242
app {
238
243
url = "https://some-url-for-the-application.dev"
239
244
}
@@ -249,11 +254,17 @@ dependency "demo_dep" {
249
254
}
250
255
```
251
256
252
-
The dependency name label *must* match the `name` property of the dependant pack, as specified in its `metadata.hcl`.
257
+
**Note**: The dependency block's label, "demo_dep" in the previous example,
258
+
*must* match the `name` property of the dependency pack, as specified in its
259
+
`metadata.hcl`.
253
260
254
-
This would allow templates of "simple_service" to use "demo_dep"'s helper templates in the following way:
261
+
For packs that reuse a dependency pack, add a unique `alias` attribute inside
262
+
each dependency block to disambiguate them.
255
263
256
-
```
264
+
This allows templates of "simple_service" to use "demo_dep"'s helper templates
265
+
in the following way.
266
+
267
+
```hcl
257
268
[[ template "demo_dep.data" . ]]
258
269
```
259
270
@@ -266,7 +277,7 @@ directory path as the name of the pack to the `run`, `plan`, `render`, `info`,
266
277
For instance, if your current working directory is the directory where you are
267
278
developing your pack, and `nomad-pack` is on your path, you can run this command.
268
279
269
-
```
280
+
```shell
270
281
nomad-pack run .
271
282
```
272
283
@@ -278,23 +289,28 @@ To use your new pack, you will likely want to publish it to the internet. Push t
278
289
accessible by your command line tool.
279
290
280
291
If you wish to share your packs, please consider adding them to the
281
-
[Nomad Pack Community Registry](https://github.com/hashicorp/nomad-pack-community-registry)
292
+
[Nomad Pack Community Registry][].
282
293
283
294
If you don't want to host a pack registry in version control, you can work locally
284
-
using the filesystem method.
295
+
using the file system method.
285
296
286
-
## Step Six: Deploy your Custom Pack!
297
+
## Step Six: Deploy your Custom Pack
287
298
288
299
Add your custom repository using the `nomad-pack registry add` command.
0 commit comments