Chef 资源,"not_if" 内的脚本不工作
Chef resource, script inside "not_if" is not working
我有一个 chef_resource 看起来像这样
bash 'regenerate-master-list' do
code <<-EOH
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml
EOH
action :run
notifies :create, 'cookbook_file[/etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml]', :immediately
not_if "diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)"
end
这个块总是执行,因为 not_if
是错误的,当我尝试手动 运行 它时,我看到了没有差异的预期结果。有什么我应该添加到脚本中的吗?
您可以尝试使用与您的 bash not_if
守卫相同的命令创建 execute
资源。
execute "diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)"
您可以直接在目标机器上的食谱缓存文件夹中执行此操作,然后使用 运行 厨师客户端 chef-client --skip-cookbook-sync
,这不会更新本地食谱缓存。 Chef 将失败并显示 运行,您将看到输出。
我的猜测是,厨师不能 运行 redis-cli,因为它不在厨师的路径中。
有点老套,但我刚刚添加了一个新的 bash 资源
# diff check_sum of current list vs. existing, drop a file for next resource to pickup
bash 'master-list-checksum' do
code <<-EOH
diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)
test $? -eq 0 || touch /etc/datadog-agent/conf.d/redis_sentinel.d/update_list
EOH
action :run
end
# remove config to be replaced with new one
bash 'regenerate-master-list' do
code <<-EOH
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/update_list
EOH
action :run
notifies :create, 'cookbook_file[/etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml]', :immediately
only_if { File.exists?("/etc/datadog-agent/conf.d/redis_sentinel.d/update_list") }
end
我有一个 chef_resource 看起来像这样
bash 'regenerate-master-list' do
code <<-EOH
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml
EOH
action :run
notifies :create, 'cookbook_file[/etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml]', :immediately
not_if "diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)"
end
这个块总是执行,因为 not_if
是错误的,当我尝试手动 运行 它时,我看到了没有差异的预期结果。有什么我应该添加到脚本中的吗?
您可以尝试使用与您的 bash not_if
守卫相同的命令创建 execute
资源。
execute "diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)"
您可以直接在目标机器上的食谱缓存文件夹中执行此操作,然后使用 运行 厨师客户端 chef-client --skip-cookbook-sync
,这不会更新本地食谱缓存。 Chef 将失败并显示 运行,您将看到输出。
我的猜测是,厨师不能 运行 redis-cli,因为它不在厨师的路径中。
有点老套,但我刚刚添加了一个新的 bash 资源
# diff check_sum of current list vs. existing, drop a file for next resource to pickup
bash 'master-list-checksum' do
code <<-EOH
diff <(cat /etc/datadog-agent/conf.d/redis_sentinel.d/master_list) <(redis-cli -p 26379 info sentinel | grep name | sed 's/,.*//' | cut -d '=' -f 2-)
test $? -eq 0 || touch /etc/datadog-agent/conf.d/redis_sentinel.d/update_list
EOH
action :run
end
# remove config to be replaced with new one
bash 'regenerate-master-list' do
code <<-EOH
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml
rm -f /etc/datadog-agent/conf.d/redis_sentinel.d/update_list
EOH
action :run
notifies :create, 'cookbook_file[/etc/datadog-agent/conf.d/redis_sentinel.d/conf.yaml]', :immediately
only_if { File.exists?("/etc/datadog-agent/conf.d/redis_sentinel.d/update_list") }
end