Ansible elasticsearch jinja2模板未正确显示

时间:2018-10-30 10:08:34

标签: elasticsearch ansible jinja2

我在jinja2中有此代码,该代码将用弹性手表制成。

jinja代码是:

search:
    request:
      indices: "{{_watch_options.input_request.indices}}"
      body:
        _source: "{{_watch_options.input_request.source}}"
        size: "{{_watch_options.input_request.size}}"
        query:
          bool:
            must:
              query_string:
                analyze_wildcard: true
                query: "{{_watch_options.input_request.query}}"
              range:
                '@timestamp':
                  "gte": "{{_watch_options.input_request.timestamp.gte}}"
                  "lte": "{{_watch_options.input_request.timestamp.lte}}"
                  "format": "epoch_millis"

结果如下:

 "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "log-*"
        ],
        "types": [],
        "body": {
          "query": {
            "bool": {
              "must": {
                "query_string": {
                  "analyze_wildcard": true,
                  "query": "NodeNotConnectedException"
                },
                "range": {
                  "@timestamp": {
                    "gte": "1535364473991",
                    "lte": "1540548473992",
                    "format": "epoch_millis"
                  }
                }
              }
            }
          },
          "size": "5",
          "_source": "elasticsearch_log.message"
        }
      }
    }
  },

但是不幸的是,query.bool.must必须设置为数组,因为否则elasticsearch无法执行query_string并添加范围。

我实际上是输入请求,而“ must”参数分别如下所示。带方括号:

        "must": [
          {
            "query_string": {
              "analyze_wildcard": true,
              "query": "NodeNotConnectedException"
            }
          },
          {
            "range": {
              "@timestamp": {
                "gte": 1535364473991,
                "lte": 1540548473992,
                "format": "epoch_millis"
              }
            }
          }
        ]

你们能提供建议ID,我可以在jinja模板中声明方括号还是强制Ansible在这种情况下使用它们?

非常感谢

1 个答案:

答案 0 :(得分:0)

如果您使用的是第一个代码段中显示的yaml,则需要在query_string:range:的前面加上一个破折号和一个空格,并在其当前缩进级别添加一个空格,以指示yaml解析器它们是must:的顺序子级,就像这样:

    query:
      bool:
        must:
          - query_string:
              # and the rest
          - range:
              # and the rest