使用jq批量修改es index template的lifecycle配置:修订间差异
来自三线的随记
小无编辑摘要 |
小无编辑摘要 |
||
| 第19行: | 第19行: | ||
es_sw_templates=`curl ${curl_options[@]} "$host/_cat/templates/sw*?s=name" | awk '{print $1}'` | es_sw_templates=`curl ${curl_options[@]} "$host/_cat/templates/sw*?s=name" | awk '{print $1}'` | ||
# bak | # bak | ||
curl ${curl_options[@]} "${host}/_index_template?pretty" > es-${host}-sw- | curl ${curl_options[@]} "${host}/_index_template?pretty" > es-${host}-sw-wildcard-templates_`date +%F%T`.bak.json | ||
for template in ${es_sw_templates[@]} | for template in ${es_sw_templates[@]} | ||
| 第28行: | 第28行: | ||
curl -X PUT -H "Content-Type: application/json" ${curl_options[@]} "${host}/_index_template/${template}" -d "${new_template}" | curl -X PUT -H "Content-Type: application/json" ${curl_options[@]} "${host}/_index_template/${template}" -d "${new_template}" | ||
echo | echo | ||
done | done | ||
[[分类:Elasticsearch]] | [[分类:Elasticsearch]] | ||
[[分类:Linux]] | [[分类:Linux]] | ||
{{DEFAULTSORT:jq}} | {{DEFAULTSORT:jq}} | ||
2023年1月24日 (二) 22:28的版本
aka: use jq tool to modify es index template lifecycle setting in bulk
新版本的es index template不支持通过结合索引匹配+优先级配置的方式来叠加批量修改已存在的索引模版(legacy template 支持),只支持通过compose实现类似用法,部分场景会带来大量的搬砖成本
引入脚本:
#!/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}-sw-wildcard-templates_`date +%F%T`.bak.json
for template in ${es_sw_templates[@]}
do
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