skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    It's a little hacky but I just added a new bash resource

    # 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
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search