Passing registered ip and port from consul to env nomad job section

Hello everyone. I have a misunderstanding about data integration from Consul to Nomad. I ask for your help. The situation is this: I have a rabbitmq service running in nomad and it is registered with Consul as a service. Through the consul I see his ip and port. I need to pass these ip and port to stanza env { } of job.nomad file for another service, what would it look like, for example:
env {
RABBITMQ_API = "some_ip:some_port(from_consul) "
}

How can this be done? I’m confused.

Hi @Vladlen. This would be solved by using a template block within your jobspec to read the service information via the consul-template service func. The env parameter on the template block can automatically export templates key/value data as environment variables.

Assuming the server within Consul is named “rabbitmq-api” the following example template block could work:

      template {
        data        = <<EOH
{{ range service "rabbitmq-api" }}
RABBITMQ_API={{ .Address }}:{{ .Port }}{{ end }}
EOH
        destination = "local/env.txt"
        env         = true
      }

Thanks,
jrasell and the Nomad team

2 Likes

@jrasell Thank you! Tell me, should I create the “local / env.txt” file somewhere separately? The point is that in my scheme there are 3 nodes, each of them has a nomad client / server, as well as a consul client. do I need to make the “local/env.txt” file available to all three nodes?

Hi @Vladlen. No you shouldn’t need to create a single file that is available to all. Each instance of the job group (allocation) will perform its own lookup and write the file to its own sandboxed filesystem. Each allocation therefore becomes an isolated and immutable object that can be replaced automatically by the scheduler.

Thanks,
jrasell and the Nomad team

1 Like

Thank you very much! I recently dived into the nomad / consul stack, and some situations are not easy for me, in terms of understanding =)

Not a problem @Vladlen; this is a good place for these types of questions, so feel free to raise more topic questions when you have them.

Thanks,
jrasell and the Nomad team

1 Like