# Declaring Variables in Logstash Config File

**URL:** <https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826>\
**Category:** Logstash\
**Created:** [May 27, 2022, 5:49pm UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826 "2022-05-27T17:49:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![usman1](https://avatars.discourse-cdn.com/v4/letter/u/c67d28/32.png) [@usman1](https://discuss.elastic.co/u/usman1)\
**Post date:** [May 27, 2022, 5:49pm UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826/1 "2022-05-27T17:49:59Z")

</div>

I am repeating a string (let's "abc-123") in my Logstash config file multiple times. I want to declare it as a variable at the start of config file and call this variable where required. Something like:

`var my_str = "abc-123"`

And use it like:  
`$my_str`

Is there a way I can do this?

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [May 27, 2022, 6:41pm UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826/2 "2022-05-27T18:41:35Z")

</div>

Can you give an example of how your config looks like?

You can't declare variables in the config file, but you can use Environment variables in some parts of the configuration, not everything.

---

<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:** [May 28, 2022, 7:45pm UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826/3 "2022-05-28T19:45:58Z")

</div>

Is this what you want?

```auto
    mutate {
        add_field => { "my_str" => "abc-123" }
    }

```

or temp variable/field:

```auto
    mutate {
        add_field => { "[@metadata][my_str]" => "abc-123" }
    }

```

---

<div class="post-metadata">

**Author:** ![usman1](https://avatars.discourse-cdn.com/v4/letter/u/c67d28/32.png) [@usman1](https://discuss.elastic.co/u/usman1)\
**Post date:** [May 30, 2022, 8:59am UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826/4 "2022-05-30T08:59:46Z")

</div>

@Rios How can we use the field `my_str` if we add it with the above syntax (the first one where we are not adding it into metadata)? I mean what would be the syntax to call `my_str` in that case?

---

<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:** [June 27, 2022, 9:00am UTC](https://discuss.elastic.co/t/declaring-variables-in-logstash-config-file/305826/5 "2022-06-27T09:00:09Z")

</div>

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