# Accessing environment variable in config file in a custom made beats

**URL:** https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241
**Category:** Beats
**Created:** [October 5, 2016, 8:22am UTC](https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241 "2016-10-05T08:22:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wing999](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@wing999](https://discuss.elastic.co/u/wing999)
#### Post date: [October 5, 2016, 8:22am UTC](https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241/1 "2016-10-05T08:22:58Z")

</div>

I have built a custom made beats based on libbeat and I want to use environment variable as a value in the config file. However, if I type ${VAR} in the config file and I run config test, I got:  
./countbeat -c countbeat.yml --configtest  
Exiting: error unpacking config data: missing field accessing 'name' (source:'countbeat.yml')

I tried in line of configuring the name of the shipper:  
name: "${HOSTNAME}"

And it works if I change it into a static string:  
name: "a\_name"

I have tried to use the filebeat (v1.2.3) and the syntax works. Have I made any mistake in building the beats? Thank you.

Thank you.

---

<div class="post-metadata">

### Author: ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)
#### Post date: [October 5, 2016, 11:58am UTC](https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241/2 "2016-10-05T11:58:51Z")

</div>

is the environment variable really set?

With beats 5.0, variable expansion will check if environment variable is available and fail if no default is set. In beats 1.x a missing environment variable will just be ignored.

this might work for you:

```auto
name: '${HOSTNAME:localhost}'

```

if HOSTNAME is not set it will expand into `"localhost"`.

In 5.0 you can try to print a custom error if HOSTNAME is missing via:

```auto
name: ${HOSTNAME:?Please set HOSTNAME environment variable}

```

in beats 5.0 one can also overwrite any setting from command line via `-E` flag. For example:

`./countbeat -c countbeat.yml --configtest -E name=otherhost`

---

<div class="post-metadata">

### Author: ![wing999](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@wing999](https://discuss.elastic.co/u/wing999)
#### Post date: [October 6, 2016, 2:36am UTC](https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241/3 "2016-10-06T02:36:54Z")

</div>

Thank you. You are right, $HOSTNAME is set in the shell only but not exported, so it is not available to countbeat.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 26, 2016, 8:23am UTC](https://discuss.elastic.co/t/accessing-environment-variable-in-config-file-in-a-custom-made-beats/62241/4 "2016-10-26T08:23:35Z")

</div>

This topic was automatically closed after 21 days. New replies are no longer allowed.
