Logstash variables definition in config file

Viewed 1789

In my Logstash config file I have many jdbc inputs and all of them use the same database and the same credentials. Each time I want to change for example connection string I have to loop through all jdbc inputs manually. Can I somehow define variables once and then use them in the config file for example like that?

CONNECTION_STRING_VARIABLE => "MY_CONNECTION_STRING"
jdbc {
      ...
      jdbc_connection_string => CONNECTION_STRING_VARIABLE
      ...
}

I don't want to use environmental variables because of user and password fields and I want to store variables in one place.

1 Answers

For passwords, you may use property jdbc_password_filepath.

I read that you don't want to set environment variables. I am giving a way here, so that, it will not be available in environment of every shell but will get loaded for logstash only.

You may create a script that exports all variables for logstash and call it in logstash service or logstash command line.

For example, create a file - exportVariablesForLogstash.sh

export jdbc_url="jdbc:mysql://example.local:3306/sampledb"
export jdbc_username=mysqluser 

Add following in starting of logstash service or logstash commandline script. Note dot at starting.

. exportVariablesForLogstash.sh 

And then you may use these variables as documented here. https://www.elastic.co/guide/en/logstash/current/environment-variables.html. I believe you already know this one.

Related