How to define sockaddr template for bind_addr in consul.hcl

Hi everyone,

I’m new to Consul and played around with for a bit. I now have a consul configuration that works for me now. I tested this configuration as configuration flags for the consul binary

consul agent
                  -bootstrap-expect 3 
                  -bind '{{ GetAllInterfaces | include "network" " " }}'
                  -datacenter dc1 
                  -data-dir /consul/data
                  -domain consul 
                  -encrypt "secretToken="
                  -retry-join "node2"
                  -retry-join "node3"

Now I would like to provide this exact config as a hcl config file. However I cannot figure out how I need to specify my sockaddr template in the hcl for consul to accept it.
When I test my template directly with sockaddr everything is fine but when I run consul validate consul.hcl I get Config validation failed: bind_addr cannot be empty

This is my consul.hcl:

bind_addr = "GetAllInterfaces | include \"network\" \"\" "
bootstrap_expect = 3
datacenter = "dc1"
data_dir = "/consul/data"
encrypt = "secretToken="
server = true
rejoin_after_leave = true
retry_join = ["node2", "node3"]

Can anyone point me in the right direction on how to specify that sockaddr template in a hcl file?

As so often, some time after you ask the question you come up with the solution yourself.

For reference here is the solution:
bind_addr: expects just an IP address. I thought that the selection of an interface was sufficient but the template has to be detailed enough to select just the IP address.

With this sockaddr template the hcl config works as expected :slight_smile:

bind_addr = "GetAllInterfaces | include \"network\" \"\ | sort \"size,address\" | attr \"address\" "

Hi @protenhan,

Thanks for posting this solution! I actually ran across this recently too - and you’re right, writing it out helps :slight_smile: :duck:

Welcome to the forums!

1 Like