Search code examples
if-statementpuppetconsulconsul-template

Consul-template if else condition


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

Solution

  • 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}}