Hi,
we are using Nomad to orchestrate our applications.
To monitor our applications we want to deploy telegraf and use the consul service registry to dynamically configure it.
Currently we do not want to deploy one central telegraf instance. Instead we would like to deploy consul per host (system job or daemonset) and have to filter to “local” consul services. In other words only monitor applications on this host.
I’m not able to dynamically filter that.
The code contains consul-template code, but I’m not able to use Nomad variables in that.
[agent]
....
hostname = "{{ env "node.unique.name" }}"
{{- range services -}}
{{- if .Name | contains "httpd-status" -}}
{{- range service .Name }}
{{ if .Node | contains "teg-dev-aio01" }}
# {{ .Name }}
[[inputs.apache]]
urls = ["http://{{ .Address }}:{{ .Port }}/server-status?auto"]
tagexclude = ["host","url"]
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
The easy way would be to use “{{ env “node.unique.name” }}” in the “.Node | contains” condition, but I cannot get the template to render correctly.
Is that possible or what do I need to change?
What do I miss?
Thanks,
Robert
Hey Robert,
You can use parentheses to wrap calls to other functions if you want to use them as input to a function. For example:
{{ if .Node | contains ( env "node.unique.name" ) }}
However, I also found in my case that node.unique.name included the “.node.consul” so I needed to switch around the sense of the comparison to:
{{ if ( env "node.unique.name" ) | contains .Node }}
Here’s the whole template job I tested with for other people who might want to experiment themselves:
job "template" {
datacenters = ["dc1"]
type = "system"
group "group" {
task "template" {
resources { memory=100 cpu=100 }
driver = "raw_exec"
config {
command = "bash"
args = ["-c", "cat local/template.out; while true; do sleep 10; done"]
}
template {
data = <<EOH
hostname = "{{ env "node.unique.name" }}"
{{- range services -}}
{{- if .Name | contains "nomad" -}}
{{- range service .Name }}
{{ if (env "node.unique.name") | contains .Node }}
# {{ .Name }}
[[inputs.apache]]
urls = ["http://{{ .Address }}:{{ .Port }}/server-status?auto"]
tagexclude = ["host","url"]
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
EOH
destination = "local/template.out"
}
}
}
}
Run nomad alloc logs on any of the allocations to see the template output.
Robert, hopefully this gets you unbound!
Best,
Charlie Voiselle