首页 > 解决方案 > 厨师中的Grep curl elasticsearch输出

问题描述

我需要卷曲 elasticsearch 节点并在厨师中 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":{}}

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

 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

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

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

知道如何在厨师中 grep 这个吗?

干杯

标签: rubybashelasticsearchchef-infra

解决方案


您需要将 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"' )

推荐阅读