如何在Terraform模板中转义Logstash sprintf表达式?

时间:2019-03-18 21:52:54

标签: logstash terraform terraform-template-file

我正在尝试使用Terraform模板文件对使用sprintf表达式的logstash配置进行模板化。

示例:

filter {
  json { source => "message" }
  geoip {
    source => "[user][rparam][ip]"
  }
  useragent {
    source => "[user][rparam][ua]"
  }
  date {
    match => [ "[@metadata][kafka][timestamp]", "UNIX_MS" ]
  }
  mutate { add_field => { "[@metadata][id]" => "%{[id]}" } }
  mutate { add_field => { "[@metadata][eid]" => "%{[eid]}" } }
  mutate { remove_field => [ "message", "id", "eid", "@version", "tags" ] }
  if "[user]" {
    mutate { remove_field => [ "[user][rparam][ip]" ] }
  }
}

不幸的是,使用的百分比字符“%”也是Terraform模板语言的一部分,因此Terraform向我返回了以下错误消息:

* data.template_file.logstash-output: data.template_file.logstash-output: failed to render : <template_file>:6,24-25: Invalid character; This character is not used within the language., and 1 other diagnostic(s)

如何才能正确逃脱? Terraform文档毫无头绪,仅指定如何对变量插值表达式($)进行转义

1 个答案:

答案 0 :(得分:1)

百分号可以用双%%排除。上面的模板文件可以这样写,并且Logstash sprintf表达式将在结果中正确处理:

filter {
  json { source => "message" }
  geoip {
    source => "[user][rparam][ip]"
  }
  useragent {
    source => "[user][rparam][ua]"
  }
  date {
    match => [ "[@metadata][kafka][timestamp]", "UNIX_MS" ]
  }
  mutate { add_field => { "[@metadata][id]" => "%%{[id]}" } }
  mutate { add_field => { "[@metadata][eid]" => "%%{[eid]}" } }
  mutate { remove_field => [ "message", "id", "eid", "@version", "tags" ] }
  if "[user]" {
    mutate { remove_field => [ "[user][rparam][ip]" ] }
  }
}