-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nomad template not reading env variables if generated dynamically. #5982
Comments
I can send you the generated file as well if you guys want. |
Thanks for reaching out. The issue here is that the template depends on consul service registration of self while it's launching and before it's marked as healthy. While zk service containers are launching and yet to be marked as healthy, If I launch another service first and wait for it to be healthy, the environment variables will be set correctly. Here, I tested registering the example The scripts to reproduce and observation: $ nomad job init --short
Example job file written to example.nomad
$ nomad job run ./example.nomad
==> Monitoring evaluation "d86a8c32"
Evaluation triggered by job "example"
Allocation "e92a9788" created: node "3b248445", group "cache"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "d86a8c32" finished with status "complete"
$ nomad job run ./zk.hcl
==> Monitoring evaluation "a1647501"
Evaluation triggered by job "zk"
Allocation "38fbb42a" created: node "3b248445", group "zk"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "a1647501" finished with status "complete"
$ nomad alloc logs 38fbb42a
>>>>>>>>>>>>>>> FROM ENV_VARS
ZOO_SIMPLE_RANGE=a
ZOO_OTHER_SERVICE=127.0.0.1
ZOO_SIMPLE_VAR=127.0.0.1
>>>>>>>>>>>>>>> FROM ENV_FILE
# test variable lookup
ZOO_SIMPLE_VAR="127.0.0.1"
# test range assignment
ZOO_SIMPLE_RANGE="a"
# test range over other services
ZOO_OTHER_SERVICE="127.0.0.1"
# test range over self services
The job file is: job "zk" {
region = "global"
datacenters = ["dc1"]
type = "service"
group "zk" {
count = 1
task "zk" {
driver = "docker"
template {
data = <<EOH
# test variable lookup
{{ $node_ip := env "NOMAD_IP_zk_port" }}ZOO_SIMPLE_VAR="{{ $node_ip }}"
# test range assignment
{{ range $i, $letters := "a" | split " " }}ZOO_SIMPLE_RANGE="{{ $letters }}"{{end}}
# test range over other services
{{ range $i, $services := service "redis-cache" }}ZOO_OTHER_SERVICE="{{ $services.Address }}"{{end}}
# test range over self services
{{range $i, $services := service "zk"}}ZOO_SELF_SERVICE="{{ $services.Address }}"{{end}}
EOH
destination = "local/zkid.env"
change_mode = "noop"
env = true
}
config {
image = "alpine:latest"
command = "/bin/sh"
args = ["-c", "echo '>>>>>>>>>>>>>>> FROM ENV_VARS'; printenv | grep ZOO_; echo; echo '>>>>>>>>>>>>>>> FROM ENV_FILE'; cat /local/zkid.env; sleep 40000"]
}
service {
name = "zk"
port = "zk_port"
address_mode = "host"
}
resources {
cpu = 1024 # 1024 Mhz
memory = 512 # 512MB
network {
port "zk_port" {}
}
}
}
}
} |
@notnoop - I tested it even with hardcoded stuff (I mean |
I have a similar issue on nomad template stanza for kibana service:
|
@lbachar thanks for the additional context! By design, consul template substitution only blocks evaluation on key value lookup, the range lookup returns immediately with whatever is available! @angrycub has an example workaround that calls If the kabana template is setup with the default Thanks, Lang |
@langmartin thanks for the quick response. I tried to find similar issues here and I found those: Looks like it was fixed before using that function: Let me know if it's better to open a new issue instead of this one. |
@langmartin Hi, indeed. I will follow #6112 |
@lbachar , yes, this issue has been reproduced and is being addressed. |
This used to work on nomad 0.9.0 but after update to 0.9.5 I get the same results: the file is generated with the service data (from |
I'm going to lock this issue because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active issues. |
Nomad version
Nomad v0.9.1 (4b2bdbd)
Operating system and Environment details
Photon OS
Issue
If we are generating the environment variables in template dynamically based of some logic then Nomad isn't attaching the env variables to task. If we provide hardcoded values the env variables values are passed to docker container properly.
Reproduction steps
Following don't work - it generates the variable in key=value format.
This works (mind the hardcoded value):
Job file (if appropriate)
Nomad Client logs (if appropriate)
Nomad Server logs (if appropriate)
The text was updated successfully, but these errors were encountered: