# Use ruby variable in if filter

**URL:** https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251
**Category:** Logstash
**Created:** [June 29, 2017, 8:58am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251 "2017-06-29T08:58:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kitz99](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kitz99](https://discuss.elastic.co/u/kitz99)
#### Post date: [June 29, 2017, 8:58am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251/1 "2017-06-29T08:58:41Z")

</div>

I have this logstash config:

```
input { stdin { } }

output {
  elasticsearch { hosts => ["localhost:9200"] }
  stdout { codec => rubydebug }
}

filter {
  ruby {
     code => "
         @@exists_pattern = ['/collaboration/display', '/collaboration/questions'].any?{ |pattern| event.get('message').include?(pattern) }
         event.add('keep_line', @@exists_pattern)
     "
  }

  if not [keep_line] { drop { } }

  grok {
match => {
  "message" => '%{IP:serverip} \[%{HTTPDATE:my_timestamp}\]'
}
  }

  date {
match => ["my_timestamp", "dd/MMM/YYYY:HH:mm:ss Z"]
target => "@timestamp"
  }
}

```

But when I try to run logstash with this config file I get this error message:

```
[ERROR][logstash.agent] Cannot create pipeline {:reason=>"Expected one of #, ( at line 30, column 10 (byte 923) after filter {\n # grok {\n

```

How can I use field 'keep\_line' in that if condition?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [June 29, 2017, 9:02am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251/2 "2017-06-29T09:02:51Z")

</div>

Yes. "keep\_line" isn't a Ruby variable, it's a field in the current event. I'm not sure what Logstash is complaining about. Judging by the error message there's something about the grok filter that it doesn't like. Try commenting out pieces of the configuration to narrow down the cause of the error.

---

<div class="post-metadata">

### Author: ![kitz99](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kitz99](https://discuss.elastic.co/u/kitz99)
#### Post date: [June 29, 2017, 9:05am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251/3 "2017-06-29T09:05:59Z")

</div>

grok seems to be ok. If I comment it, I still get this error:

[ERROR][logstash.agent] Cannot create pipeline {:reason=\>"Expected one of #, ( at line 16, column 10 (byte 350) after filter {\n ruby {\n code =\> "\n @@exists\_pattern = ['/collaboration/display', '/collaboration/questions'].any?{ |pattern| event.get('message').include?(pattern) }\n event.add('keep\_line', @@exists\_pattern)\n "\n }\n\n if not "}  
2017-06-29 12:05:03,847 Api Webserver ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console.

---

<div class="post-metadata">

### Author: ![kitz99](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kitz99](https://discuss.elastic.co/u/kitz99)
#### Post date: [June 29, 2017, 9:42am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251/4 "2017-06-29T09:42:05Z")

</div>

The problem seems to be with `not [keep_line]`. I switched to `if [keep_line]` and logstash started. I keep digging

---

<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: [July 27, 2017, 9:42am UTC](https://discuss.elastic.co/t/use-ruby-variable-in-if-filter/91251/5 "2017-07-27T09:42:31Z")

</div>

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