# Conditional on elasticsearch output not working as expected

**URL:** <https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772>\
**Category:** Logstash\
**Created:** [August 26, 2019, 1:02pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772 "2019-08-26T13:02:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [August 26, 2019, 1:02pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772/1 "2019-08-26T13:02:34Z")

</div>

Hi folks,  
I'm using the elasticsearch output plugin, and I want to define two alternatives configurations for it, depending on a certain condition (see code below).

```
...
output {
  if [condition] {
    elasticsearch {
      hosts => ["condition-met-host"]
      index => "an-index-name"
      ...
    }
  } else {
    elasticsearch {
      hosts => ["condition-not-met-host"]
      index => "an-index-name"
      ...
    }
  }
}

```

Everything seems to work as expected as long as the `hosts` (or `proxy`) property of BOTH the output plugins refers to a valid Elasticsearch host. However, in practice, Logstash attempts to establish a connection also with the host of the branch that doesn't satisfy the condition. If such a host exists, no problems, you wouldn't even notice. On the contrary, if such a host doesn't exist (and this is exactly what my condition is there for), Logstash keeps trying to resurrect the connection, preventing the pipeline to terminate:

> [WARN][logstash.outputs.elasticsearch] Attempted to resurrect connection to dead ES instance, but got an error. {:url=\>"[http://condition-not-met-host:9200/](http://condition-not-met-host:9200/)", :error\_type=\>LogStash::Outputs::Elasticsearch::HttpClient::Pool::HostUnreachableError, :error=\>"Elasticsearch Unreachable: [[http://condition-not-met-host:9200/](http://condition-not-met-host:9200/)][Manticore::ResolutionFailure] condition-not-met-host: Name or service not known"}

Am I doing something wrong? Am I missing something important?

- thanks 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:** [August 26, 2019, 1:40pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772/2 "2019-08-26T13:40:58Z")

</div>

That's exactly what I would expect. logstash establishes a connection during initialization, and if initialization never completes then shutdown will hang.

---

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [August 26, 2019, 1:51pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772/3 "2019-08-26T13:51:09Z")

</div>

Thanks for the reply. Then, the expected behaviour is to initialise all the plugins, regardless of whether they are defined in a conditional branch that will never be executed. Is that correct?

If that's the case, I'll need a different solution for my problem, maybe separated pipelines.

---

<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:** [August 26, 2019, 1:54pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772/4 "2019-08-26T13:54:32Z")

</div>

> [@glenacota](#):
>
> Then, the expected behaviour is to initialise all the plugins, regardless of whether they are defined in a conditional branch that will never be executed.

That is correct.

---

<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:** [September 23, 2019, 1:54pm UTC](https://discuss.elastic.co/t/conditional-on-elasticsearch-output-not-working-as-expected/196772/5 "2019-09-23T13:54:32Z")

</div>

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