mirror of https://github.com/hashicorp/packer
commit
df89b4b52c
@ -1,11 +0,0 @@
|
||||
---
|
||||
page_title: Tips and Tricks
|
||||
description: |-
|
||||
The guides stored in this section are miscellanious tips and tricks that might
|
||||
make your experience of using Packer easier
|
||||
---
|
||||
|
||||
# Tips and Tricks
|
||||
|
||||
Click the sidebar navigation to check out the miscellaneous guides we have for
|
||||
making your life with Packer just a bit easier.
|
||||
@ -1,84 +0,0 @@
|
||||
---
|
||||
page_title: Using the isotime template function - Guides
|
||||
description: |-
|
||||
It can be a bit confusing to figure out how to format your isotime using the
|
||||
golang reference date string. Here is a small guide and some examples.
|
||||
---
|
||||
|
||||
# Using the Isotime template function with a format string
|
||||
|
||||
The way you format isotime in golang is a bit nontraditional compared to how
|
||||
you may be used to formatting datetime strings.
|
||||
|
||||
Full docs and examples for the golang time formatting function can be found
|
||||
[here](https://golang.org/pkg/time/#example_Time_Format)
|
||||
|
||||
However, the formatting basics are worth describing here. From the [golang docs](https://golang.org/pkg/time/#pkg-constants):
|
||||
|
||||
> These are predefined layouts for use in Time.Format and time.Parse. The
|
||||
> reference time used in the layouts is the specific time:
|
||||
>
|
||||
> Mon Jan 2 15:04:05 MST 2006
|
||||
>
|
||||
> which is Unix time 1136239445. Since MST is GMT-0700, the reference time
|
||||
> can be thought of as
|
||||
>
|
||||
> 01/02 03:04:05PM '06 -0700
|
||||
>
|
||||
> To define your own format, write down what the reference time would look like
|
||||
> formatted your way; see the values of constants like ANSIC, StampMicro or
|
||||
> Kitchen for examples. The model is to demonstrate what the reference time
|
||||
> looks like so that the Format and Parse methods can apply the same
|
||||
> transformation to a general time value.
|
||||
|
||||
So what does that look like in a Packer template function?
|
||||
|
||||
```json
|
||||
{
|
||||
"variables": {
|
||||
"myvar": "packer-{{isotime \"2006-01-02 03:04:05\"}}"
|
||||
},
|
||||
"builders": [
|
||||
{
|
||||
"type": "null",
|
||||
"communicator": "none"
|
||||
}
|
||||
],
|
||||
"provisioners": [
|
||||
{
|
||||
"type": "shell-local",
|
||||
"inline": ["echo {{ user `myvar`}}"]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
You can switch out the variables section above with the following examples to
|
||||
get different timestamps:
|
||||
|
||||
Date only, not time:
|
||||
|
||||
```json
|
||||
"variables":
|
||||
{
|
||||
"myvar": "packer-{{isotime \"2006-01-02\"}}"
|
||||
}
|
||||
```
|
||||
|
||||
A timestamp down to the millisecond:
|
||||
|
||||
```json
|
||||
"variables":
|
||||
{
|
||||
"myvar": "packer-{{isotime \"Jan-_2-15:04:05.000\"}}"
|
||||
}
|
||||
```
|
||||
|
||||
Or just the time as it would appear on a digital clock:
|
||||
|
||||
```json
|
||||
"variables":
|
||||
{
|
||||
"myvar": "packer-{{isotime \"3:04PM\"}}"
|
||||
}
|
||||
```
|
||||
@ -1,75 +0,0 @@
|
||||
---
|
||||
page_title: Use jq and Packer to comment your templates - Guides
|
||||
description: |-
|
||||
You can add detailed comments beyond the root-level underscore-prefixed field
|
||||
supported by Packer, and remove them using jq.
|
||||
---
|
||||
|
||||
# How to use jq to strip unsupported comments from a Packer template
|
||||
|
||||
-> **Note:** Packer supports HCL2 from version 1.6.0, the Hashicorp Configuration
|
||||
Language allows to comment directly in template files. Consider upgrading your
|
||||
JSON template to HCL2 using the `packer hcl2_upgrade` command.
|
||||
|
||||
One of the biggest complaints we get about Packer is that JSON doesn't use comments.
|
||||
For Packer JSON templates, you can add detailed comments beyond the root-level underscore-prefixed field supported by Packer, and remove them using jq.
|
||||
|
||||
Let's say we have a file named `commented_template.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"_comment": ["this is", "a multi-line", "comment"],
|
||||
"builders": [
|
||||
{
|
||||
"_comment": "this is a comment inside a builder",
|
||||
"type": "null",
|
||||
"communicator": "none"
|
||||
}
|
||||
],
|
||||
"_comment": "this is a root level comment",
|
||||
"provisioners": [
|
||||
{
|
||||
"_comment": "this is a different comment",
|
||||
"type": "shell",
|
||||
"_comment": "this is yet another comment",
|
||||
"inline": ["echo hellooooo"]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
```shell-session
|
||||
$ jq 'walk(if type == "object" then del(._comment) else . end)' commented_template.json > uncommented_template.json
|
||||
```
|
||||
|
||||
will produce a new file containing:
|
||||
|
||||
```json
|
||||
{
|
||||
"builders": [
|
||||
{
|
||||
"type": "null",
|
||||
"communicator": "none"
|
||||
}
|
||||
],
|
||||
"provisioners": [
|
||||
{
|
||||
"type": "shell",
|
||||
"inline": ["echo hellooooo"]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Once you've got your uncommented file, you can call `packer build` on it like
|
||||
you normally would.
|
||||
|
||||
## The walk function
|
||||
|
||||
If your install of jq does not have the walk function and you get an error like
|
||||
|
||||
```text
|
||||
jq: error: walk/1 is not defined at <top-level>,
|
||||
```
|
||||
|
||||
You can create a file `~/.jq` and add the [walk function](https://github.com/stedolan/jq/blob/ad9fc9f559e78a764aac20f669f23cdd020cd943/src/builtin.jq#L255-L262) to it by hand.
|
||||
Loading…
Reference in new issue