3

I am using env variables in filebeat.yml, it is failing to parse the variables.

filebeat.yml

 output.elasticsearch:
 hosts: [$ELASTICSEARCH_HOST]
 template:
  name: "filebeat"
  path: "fields.yml"
 overwrite: false
 protocol: "http"

docker-compose.yml

  version: "3.5"

  services:

   filebeat:
       build:
       context: ./filebeat
       args:
        ELK_VERSION: $ELK_VERSION
       volumes:
           - "/var/lib/docker/containers:/usr/share/dockerlogs/data:ro"
           - "/var/run/docker.sock:/var/run/docker.sock"

   networks:
    default:
     name: filebeat-nw
     external: true

I exported the variable $ELASTICSEARCH_HOST to the environment variables. However it is failing to parse the document.

I am running the container as a service with the command "docker-compose up --build"

I want to understand how to use environment variables in filebeat.yml file.

Thank You.

BSM
  • 173
  • 3
  • 13

2 Answers2

2

Each variable reference is replaced at startup by the value of the environment variable. The replacement is case-sensitive and occurs before the YAML file is parsed. References to undefined variables are replaced by empty strings unless you specify a default value. To specify a default value, use:

${VAR:default_value}

Full documentation is here

ozlevka
  • 1,988
  • 16
  • 28
2

I found the answer on this reference page

To make the environment variable accessible by the Filebeat configuration file, you need to define it with the environment setting in docker-compose.yml

docker-compose.yml

# other settings omitted
services:
  filebeat:
    environment:
      ELASTICSEARCH_HOSTS: "<host1>:<port1>,<host2>:<port2>"

Then in filebeat.yml:

# other settings omitted
output.elasticsearch:
  hosts: '${ELASTICSEARCH_HOSTS}'

Hope this solves your problem!

Tom Huang
  • 21
  • 2