# Using variables when registering services with consul

**URL:** <https://discuss.hashicorp.com/t/using-variables-when-registering-services-with-consul/37811>\
**Category:** Consul\
**Created:** [March 31, 2022, 10:03am UTC](https://discuss.hashicorp.com/t/using-variables-when-registering-services-with-consul/37811 "2022-03-31T10:03:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaci](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/vaci/32/27125_2.png) [@vaci](https://discuss.hashicorp.com/u/vaci)\
**Post date:** [March 31, 2022, 10:03am UTC](https://discuss.hashicorp.com/t/using-variables-when-registering-services-with-consul/37811/1 "2022-03-31T10:03:06Z")

</div>

Is it possible to use variables when registering services with consul using the CLI and an HCL file?

I’d like to write a service definition such as:

```auto
variable "foo" {
  type = string
  default = "bar"
}

service {
  name = var.foo
  port = 7000
}

```

---

<div class="post-metadata">

**Author:** ![Wolfsrudel](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/wolfsrudel/32/28465_2.png) [@Wolfsrudel](https://discuss.hashicorp.com/u/Wolfsrudel)\
**Post date:** [April 2, 2022, 2:09pm UTC](https://discuss.hashicorp.com/t/using-variables-when-registering-services-with-consul/37811/2 "2022-04-02T14:09:31Z")

</div>

See the docs: [Agents - Configuration File Reference | Consul | HashiCorp Developer](https://www.consul.io/docs/agent/options#environment-variables)

I quote:

> Environment variables cannot be used to configure the Consul client. They can be used when running other consul CLI commands that connect with a running agent, e.g. CONSUL\_HTTP\_ADDR=192.168.0.1:8500 consul members.

As a hack you could use `envsubst` in a wrapper before starting Consul to manipulate the configuration file.

> **[envsubst Invocation (GNU gettext utilities)](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html)**
>
> envsubst Invocation (GNU gettext utilities)
