BSM
BSM

Reputation: 183

How to use variables in filebeat.yml file

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.

Upvotes: 3

Views: 6492

Answers (2)

Tom Huang
Tom Huang

Reputation: 31

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!

Upvotes: 3

ozlevka
ozlevka

Reputation: 2146

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

Upvotes: 2

Related Questions