hashicorp / hashicorp/nomad

Weird error with Nomad Variables and templates when trying to toJSONPretty

Open
#17,026 2 comments 2 reactions 0 assignees View on GitHub
stage/accepted theme/template type/bug
Dominant language
Go
Stars
17k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
105

Description

### Nomad version
`Nomad v1.5.3` (client & server)

### Issue
I'm trying to generate a file via `template{}` that has the exact same contents as my Nomad Variable.

Here's my variable data at `nomad/jobs/somejob`:
```json
{
"baz": "qux",
"foo": "bar"
}
```

Here's my template
```hcl
template {
data = <<-EOF
{{ nomadVar "nomad/jobs/somejob" | toJSON }}
EOF
destination = "local/output"
}
```
The above works just as expected, and the contents of `local/output` is:

```json
{"baz":"qux","foo":"bar"}
```

However, when switching `toJSON` to `toJSONPretty` I get the following error (allocation):

```
Template failed: (dynamic): execute: template: :2:7: executing "" at : wrong type for value; expected map[string]interface {}; got *dependency.NomadVarItems
```

Which is super weird because why would a pretty json be any different from just regular json?

I went ahead and tested it with latest consul-template (v0.31.0) and it seems to work just fine:

```shell
$ cat in.tpl
{{ with nomadVar "nomad/jobs/somejob" }}
{{ . | toJSONPretty }}
{{ end }}

$ consul-template -once -dry -template=in.tpl
> out.json

{
"baz": "qux",
"foo": "bar"
}
```

### Reproduction steps

1. Add variables to `nomad/jobs/somejob`
2. Run the job bellow (uncomment to see it breaking):

```hcl
job "somejob" {
group "somejob" {
task "somejob" {
driver = "docker"

config {
image = "busybox:latest"
command = "sh"
args = ["-c", "while true; do echo . && sleep 5; done"]
}

template {
data = <<-EOF
{{ nomadVar "nomad/jobs/somejob" | toJSON }}
EOF

destination = "local/flat.json"
}

# Uncomment below to let it start failing
#
# template {
# data = <<-EOF
# {{ nomadVar "nomad/jobs/somejob" | toJSONPretty }}
# EOF

# destination = "local/pretty.json"
# }
}
}
}
```

#### Expected Result
`toJSON` and `toJSONPretty` working the same when combined with `NomadVarItems`.

#### Actual Result
`toJSONPretty` doesn't work (only in Nomad).

#### Workarounds

```
{{ nomadVar "path" | toJSON | parseJSON | toJSONPretty }}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the provided Nomad job reproduction and compare the template behavior of toJSON and toJSONPretty when given nomadVar output. Confirm the issue against Nomad v1.5.3 or a current build, then trace the template function handling; done means both functions accept NomadVarItems consistently without requiring the parseJSON workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, json
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.