# Set variable in configuration file

**URL:** <https://discuss.elastic.co/t/set-variable-in-configuration-file/316096>\
**Category:** Logstash\
**Created:** [October 7, 2022, 3:28pm UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096 "2022-10-07T15:28:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RickT](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@RickT](https://discuss.elastic.co/u/RickT)\
**Post date:** [October 7, 2022, 3:28pm UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/1 "2022-10-07T15:28:18Z")

</div>

Hi all !

Simply, is there some method in Logstash news versions to code a variable which can be used in input, filter and output sections ?  
if so, how to code it ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)\
**Post date:** [October 7, 2022, 7:49pm UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/2 "2022-10-07T19:49:43Z")

</div>

Yes.

```auto
input {
  anypluginname{
 ... some code...
  add_field => {"yourvariable" => "values"} 
  }
}

```

Another option is [environment variables](https://www.elastic.co/guide/en/logstash/current/environment-variables.html).

---

<div class="post-metadata">

**Author:** ![RickT](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@RickT](https://discuss.elastic.co/u/RickT)\
**Post date:** [October 10, 2022, 8:33am UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/3 "2022-10-10T08:33:30Z")

</div>

Thanks, but...

in my simple example below, it doesn't quite work! ☹

look at this :

```auto
input {
	file {
		 add_field => {"dbfile" => "reffile"}
         ...
		 sincedb_path => "D:/db_logstash/%{dbfile}.db"
	     ...
	}
}

```

After logstash exec, the file D:/db\_logstash/ **%{dbfile}.db** has been created !?  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/b/ab77047d5b250616370b60badf621a997d5b431a.png)

And not D:/db\_logstash/reffile.db, as I wanted!  
Maybe a syntax error? 🤔

---

<div class="post-metadata">

**Author:** ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)\
**Post date:** [October 10, 2022, 8:54am UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/4 "2022-10-10T08:54:52Z")

</div>

I think this is not support what you want.

---

<div class="post-metadata">

**Author:** ![RickT](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@RickT](https://discuss.elastic.co/u/RickT)\
**Post date:** [October 10, 2022, 9:11am UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/5 "2022-10-10T09:11:03Z")

</div>

Too bad ☹  
Thanks for the heads up!

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [October 10, 2022, 3:41pm UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/6 "2022-10-10T15:41:30Z")

</div>

You cannot use a sprintf reference in an input configuration because at the time that the input configuration is compiled and processed there are no events, so there are no fields to reference.

The add\_field option only takes effect one the input is configured and starts generating events.

---

<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:** [November 7, 2022, 3:41pm UTC](https://discuss.elastic.co/t/set-variable-in-configuration-file/316096/7 "2022-11-07T15:41:52Z")

</div>

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