使用jq批量修改es index template的lifecycle配置:修订间差异

来自三线的随记
无编辑摘要
无编辑摘要
 
(未显示同一用户的3个中间版本)
第1行: 第1行:
aka: use jq tool to modify es index template lifecycle setting in bulk
aka: use jq tool to modify es index template lifecycle setting in bulk


新版本的es index template不支持通过结合索引匹配+优先级配置的方式来叠加批量修改已存在的索引模版(legacy template 支持),只支持通过compose实现类似用法,部分场景会带来大量的搬砖成本(例如修改skywalking的索引模板中的生命周期配置)
新版本的es index template不支持通过结合索引匹配+优先级配置的方式来叠加批量修改已存在的索引模版(legacy template 支持),只支持通过compose实现类似用法,部分场景会带来大量的搬砖成本(例如修改skywalking的索引模板中的生命周期配置或者索引副本数配置)


引入脚本:
引入脚本:<pre>
#!/bin/bash
<nowiki>
#set -exu
#!/bin/bash
set -ue
#set -exu
host="127.0.0.1:9200"
set -ue
username="elastic"
host="127.0.0.1:9200"
password="elastic"
username="elastic"
ilm_name="sw-default-ilm"
password="elastic"
ilm_name="sw-default-ilm"
curl_options=(
 
  "-u" "$username:$password"
curl_options=(
  "-s"
  "-u" "$username:$password"
)
  "-s"
)
es_sw_templates=`curl ${curl_options[@]} "$host/_cat/templates/sw*?s=name" | awk '{print $1}'`
 
# bak
es_sw_templates=`curl ${curl_options[@]} "$host/_cat/templates/sw*?s=name" | awk '{print $1}'`
curl ${curl_options[@]} "${host}/_index_template?pretty" > es-${host}-index-templates_`date +%F%T`.bak.json
# bak
curl ${curl_options[@]} "${host}/_index_template?pretty" > es-${host}-index-templates_`date +%F%T`.bak.json
for template in ${es_sw_templates[@]}
 
do
for template in ${es_sw_templates[@]}
  echo PUT /_index_template/${template}
do
  new_template=""
  if [[ $template == "sw_segment" ]]
  new_template=`curl ${curl_options[@]} "${host}/_index_template/${template}" | jq '.index_templates[0].index_template |  .template.settings.index.lifecycle.name |= "'${ilm_name}'"'`
  then
  curl -X PUT -H "Content-Type: application/json" ${curl_options[@]} "${host}/_index_template/${template}" -d "${new_template}"
    continue
  echo
  fi
done
  echo PUT /_index_template/${template}
  new_template=""
  new_template=`curl ${curl_options[@]} "${host}/_index_template/${template}" | jq '.index_templates[0].index_template |  .template.settings.index.lifecycle.name |= "'${ilm_name}'"'`
  curl -X PUT -H "Content-Type: application/json" ${curl_options[@]} "${host}/_index_template/${template}" -d "${new_template}"
  echo
done
</nowiki></pre>
[[分类:Elasticsearch]]
[[分类:Elasticsearch]]
[[分类:Linux]]
[[分类:Linux]]
也可以将修改 lifecycle.name 改为修改 compose 以及修改 refresh_interval
new_template=`curl ${curl_options[@]} "${host}/_index_template/${template}" | jq '.index_templates[0].index_template |  .composed_of = ["sw_default_ilm"] | .template.settings.index.refresh_interval = "180s"'`
{{DEFAULTSORT:jq}}
{{DEFAULTSORT:jq}}

2025年8月22日 (五) 16:56的最新版本

aka: use jq tool to modify es index template lifecycle setting in bulk

新版本的es index template不支持通过结合索引匹配+优先级配置的方式来叠加批量修改已存在的索引模版(legacy template 支持),只支持通过compose实现类似用法,部分场景会带来大量的搬砖成本(例如修改skywalking的索引模板中的生命周期配置或者索引副本数配置)

引入脚本:


#!/bin/bash
#set -exu
set -ue
host="127.0.0.1:9200"
username="elastic"
password="elastic"
ilm_name="sw-default-ilm"

curl_options=(
  "-u" "$username:$password"
  "-s"
)

es_sw_templates=`curl ${curl_options[@]} "$host/_cat/templates/sw*?s=name" | awk '{print $1}'`
# bak
curl ${curl_options[@]} "${host}/_index_template?pretty" > es-${host}-index-templates_`date +%F%T`.bak.json

for template in ${es_sw_templates[@]}
do
  if [[ $template == "sw_segment" ]]
  then
    continue
  fi
  echo PUT /_index_template/${template}
  new_template=""
  new_template=`curl ${curl_options[@]} "${host}/_index_template/${template}" | jq '.index_templates[0].index_template |  .template.settings.index.lifecycle.name |= "'${ilm_name}'"'`
  curl -X PUT -H "Content-Type: application/json" ${curl_options[@]} "${host}/_index_template/${template}" -d "${new_template}"
  echo
done

也可以将修改 lifecycle.name 改为修改 compose 以及修改 refresh_interval

new_template=`curl ${curl_options[@]} "${host}/_index_template/${template}" | jq '.index_templates[0].index_template |  .composed_of = ["sw_default_ilm"] | .template.settings.index.refresh_interval = "180s"'`