# Using list environment variables in filebeat config

**URL:** https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124
**Category:** Beats
**Tags:** filebeat
**Created:** [December 28, 2016, 9:51am UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124 "2016-12-28T09:51:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ApsOps](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apsops/32/14180_2.png) [@ApsOps](https://discuss.elastic.co/u/ApsOps)
#### Post date: [December 28, 2016, 9:51am UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/1 "2016-12-28T09:51:17Z")

</div>

Previous discussion at [Interpolate list from environment variable in filebeats](https://discuss.elastic.co/t/interpolate-list-from-environment-variable-in-filebeats/65517) and [https://github.com/elastic/beats/pull/3045](https://github.com/elastic/beats/pull/3045)

I built from master, so this^ PR is included.

I'm using this config:

```auto
output.logstash:
    hosts: '${LOGSTASH_HOSTS:"logstash:5043"}'
    timeout: 15

```

Using `LOGSTASH_HOSTS="host1:5043,host2:5043"` gives an error:

```auto
failed to initialize logstash plugin as output: can not convert 'object' into 'string' accessing 'output.logstash.hosts'

```

* * *

In v1.2.x, this works with `LOGSTASH_HOSTS="'host1:4083','host2:4084'"` and following config:

```auto
output.logstash:
    hosts: [${LOGSTASH_HOSTS:'logstash:5043'}]

```

What is the correct way to using these type of environment variables now?

---

<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: [December 29, 2016, 12:25am UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/2 "2016-12-29T00:25:11Z")

</div>

Hi,

Thanks for continuing the discussion here 😉

Doing some more tests it seems your hitting a bug in go-ucfg itself. A bug fix should be available in this [PR](https://github.com/elastic/go-ucfg/pull/80).

You can try (and confirm fix works) by checking out the branch and recompile. Steps to test the PR with beats:

```auto
# 1. remove the vendored go-ucfg
$ cd $GOPATH/github.com/elastic/beats/vendor/github.com/elastic
$ rm -fR go-ucfg

# 2. clone and check out go-ucfg branch including fix
$ cd $GOPATH/github.com/elastic
$ git clone https://github.com/urso/go-ucfg.git
$ cd go-ucfg
$ git checkout enh/parse

# 3. rebuild filebeat
$ cd $GOPATH/github.com/elastic/beats/filebeat
$ make

```

The PR includes new tests, checking for your exact use-case: [https://github.com/urso/go-ucfg/blob/enh/parse/structs\_test.go#L54](https://github.com/urso/go-ucfg/blob/enh/parse/structs_test.go#L54)

With this change config should be:

filebeat.yml

```auto
output.logstash:
  hosts: '${LOGSTASH_HOSTS:localhost:5043}'

```

Using `LOGSTASH_HOSTS="host1:5043,host2:5043"` should make it work.

In 5.x support for env-vars has been changed and expanded. In 1.x release, env variables have been expanded like string-templates, potentially resulting in YAML parsing errors (with potentially wrong line number). Plus, silently dropping configs if environment variable was missing. In 5.x variable expansion was introduced and more sophisticated variable parsing from configuration itself (after parsing). Variable expansion + parsing + support for overwriting settings in other config files or from command line + referencing other settings in any config file or environment variable + custom error message unfortunately doesn't mix well with plain template like text replacement.

You can for example print an error message if not logstash output is set:

```auto
output.logstash:
  hosts: '${LOGSTASH_HOSTS:?No logstash host configured}'

```

and overwrite the hosts from command line `-E output.logstash.hosts=host1:5043,host2:5043`

Or have a configure default host:

```auto
default.host=${TEST_HOST:localhost}

output.logstash:
  hosts: '${LOGSTASH_HOSTS:${default.host}:5043}'

```

and change default host from command line using `-E default.host=other-host`.

The 5.x config file format is documented here: [https://www.elastic.co/guide/en/beats/libbeat/current/config-file-format.html](https://www.elastic.co/guide/en/beats/libbeat/current/config-file-format.html)

Unfortunately the section on environment variable support is still lacking ☹

---

<div class="post-metadata">

### Author: ![ApsOps](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apsops/32/14180_2.png) [@ApsOps](https://discuss.elastic.co/u/ApsOps)
#### Post date: [December 29, 2016, 5:59am UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/3 "2016-12-29T05:59:10Z")

</div>

Thanks a lot for helping out.

Building go-ucfg fix from the branch works!

> [@steffens](#):
>
> You can for example print an error message if not logstash output is set:
> 
> output.logstash:  
> hosts: '${LOGSTASH\_HOSTS:?No logstash host configured}'
> 
> and overwrite the hosts from command line -E output.logstash.hosts=host1:5043,host2:5043

I'm not able to get this^ working though. It fails, but the custom error message is not printed.

---

<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: [December 29, 2016, 3:08pm UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/4 "2016-12-29T15:08:16Z")

</div>

Thanks for testing.

You're exactly hitting this line:

```auto
    // TODO: improve error message

```

I just pushed another commit to the branch addressing the TODO.

It's supposed to print:

```auto
ERR failed to initialize logstash plugin as output: No logstash host configured accessing 'output.logstash.hosts' (source:'filebeat.yml')

```

Doing this (testing with metricbeat) works for me (metricbeat starts up and uses both hosts):

```auto
./metricbeat -e -v -c metricbeat.yml -E output.logstash.hosts=localhost:1234,localhost:5678

```

Maybe you have to put the hosts in quotes (I'm using the zsh - z shell)? Also do not forget the space between `-E` and the setting to overwrite hosts from CLI.

For testing I often have another config file with outputs. E.g. I run filebeat like this:

```auto
filebeat -e -v -c filebeat.yml -c outputs.yml

```

---

<div class="post-metadata">

### Author: ![ApsOps](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apsops/32/14180_2.png) [@ApsOps](https://discuss.elastic.co/u/ApsOps)
#### Post date: [December 29, 2016, 7:04pm UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/5 "2016-12-29T19:04:39Z")

</div>

Awesome! The error message is shown correctly with the update.

Thanks again 🙂

---

<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: [January 26, 2017, 7:05pm UTC](https://discuss.elastic.co/t/using-list-environment-variables-in-filebeat-config/70124/6 "2017-01-26T19:05:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
