Search code examples
bashchef-infra

Chef resource, script inside "not_if" is not working


I have a chef_resource that looks like this

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

this block always executes because the not_if is falsey, when I try to run it manually I see the expected results which is no diff. Is there anything I should add to the script?


Solution

  • You can try to create an execute resource with command equal to your bash not_if guard.

    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-)"
    

    You can do it right on the target machine in cookbook cache folder, and then run chef client with chef-client --skip-cookbook-sync, which does not update local cookbook cache. Chef will fail with the run and you will see the output.

    My guess is, chef cannot run redis-cli, because it is not in PATH for chef.