Chef 中的 Grep curl elasticsearch 输出

Grep curl elasticsearch output in chef

我需要卷曲 elasticsearch 节点并在 chef 中 grep 结果。

当我尝试在盒子上手动执行时,效果很好 - 突出显示我需要的内容:

curl -u 'elastic:xxxxxx' -XGET https://x.x.x.x:9201/_cluster/settings | grep '"node_concurrent_recoveries"\:"100"'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   141  100   141    0     0  14100      0 --:--:-- --:--:-- --:--:-- 14100
{"persistent":{"cluster":{"routing":{"allocation":{"cluster_concurrent_rebalance":"55","node_concurrent_recoveries":"100"}}}},"transient":{}}

但是当我尝试在 Chef 中使用它时,它只是忽略了它:

 bash 'set-concurrent-restores-to-100' do
    user 'root'
    code <<-EOC
    echo "setting concurrent restores to 100..."
    curl -u 'elastic:#{node['ElasticPassword']}' -XPUT -H 'Content-Type: application/json' 'https://#{node['fqdn']}:9201/_cluster/settings' -d '{ "persistent": { "cluster.routing.allocation.node_concurrent_recoveries": "100" } }'
    EOC
    not_if "curl -u 'elastic:xxxxx' -XGET https://#{node['fqdn']}:9201/_cluster/settings | grep ''node_concurrent_recoveries'\:'100'' "
  end

Chef 继续执行代码,即使 not_if 语句中的 curl 命令运行良好。

当我尝试仅对“100”进行 grep 时,not_if 语句运行良好。因此,在我的例子中,grep 多个字符串肯定存在问题 "node_concurrent_recoveries":"100" .

知道如何在 Chef 中 grep 这个吗?

干杯

您需要用双引号替换 grep (grep ''node_concurrent_recoveries'\:'100'') 中的单引号,因为 json 中有双引号。您可以尝试使用 ruby % string literal,这样就不需要转义很多引号了。

not_if %Q(curl -u 'elastic:xxxxx' -XGET https://#{node['fqdn']}:9201/_cluster/settings | grep '"node_concurrent_recoveries":"100"' )