# Set a environment variable as a part of the path in Output file

**URL:** <https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238>\
**Category:** Logstash\
**Created:** [September 26, 2019, 1:05pm UTC](https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238 "2019-09-26T13:05:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nahiko2000](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nahiko2000](https://discuss.elastic.co/u/nahiko2000)\
**Post date:** [September 26, 2019, 1:05pm UTC](https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238/1 "2019-09-26T13:05:23Z")

</div>

Hi!

I have two logstash nodes in a cluster  
I want both Logstash to output a txt file with a name something like

```
index_name-2019.09-node1

```

being "node1" the HOSTNAME of the logstash node. So each logstash node would output a diferent file name.

I try

```
@echo HOSTNAME

```

and I have this result

```
logstash_node_1

```

My logstash output config looks like

```
output {
    file {
			id => "outputTest"
			path => "/test/index_name-%{+YYYY-MM}_${HOSTNAME:12345}.txt"
			codec => line { format => "%{message}"}
		}
	}		

```

However, the text file name it creates is

```
index_name-2019.09_12345.txt

```

I start logstash as a service by typing:

```
sudo systemctl start logstash.service

```

Thanks a lot in advance!

---

<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:** [September 26, 2019, 2:08pm UTC](https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238/2 "2019-09-26T14:08:13Z")

</div>

Although there is a HOSTNAME environment variable for login shells, that does not mean it is set for services. [This](https://superuser.com/questions/968561/how-to-get-the-machine-ip-address-in-a-systemd-service-file) explains how to get an environment variable set for a service.

---

<div class="post-metadata">

**Author:** ![nahiko2000](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nahiko2000](https://discuss.elastic.co/u/nahiko2000)\
**Post date:** [September 27, 2019, 7:09am UTC](https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238/3 "2019-09-27T07:09:30Z")

</div>

Hi Badger!

Thanks a lot, you were right, I modified logstash.service, adding this line in the [Service] section:

```
Environment="logstashnode=n1"

```

And now in the pipeline set:

```
path => "/test/index_name-%{+YYYY-MM}_${logstashnode}.txt"

```

Now, the file it creates is named:

```
index_name-2019.09_n1.txt

```

Thank you very much!

---

<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:** [October 25, 2019, 7:09am UTC](https://discuss.elastic.co/t/set-a-environment-variable-as-a-part-of-the-path-in-output-file/201238/4 "2019-10-25T07:09:35Z")

</div>

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