Consul-template if else condition

Kasun Rathnayaka picture Kasun Rathnayaka · Jul 24, 2017 · Viewed 7k times · Source

I have below consul-template.

{{ range service "mysql_slave.mysql" "any" }}
host_name                      {{.Node}}
command                        check_nrpe!check_procs_1
{{end}}

I want to add if my hostname match "database-1" then command "check_procs_1" and others command "check_procs_2"

output

host_name                      node_server
command                        check_nrpe!check_procs_2

host_name                      database-1
command                        check_nrpe!check_procs_1

host_name                      webserver
command                        check_nrpe!check_procs_2

Answer

Kasun Rathnayaka picture Kasun Rathnayaka · Jul 26, 2017

To solve this issue we can use below fix.

{{ range service "mysql_slave.mysql" "any" }}

  {{ if eq .Node "database-1" }}

  host_name                      {{.Node}}
  command                        check_nrpe!check_procs_1

  {{else}}

  host_name                      {{.Node}}
  command                        check_nrpe!check_procs_2

  {{end}}

{{end}}