Question Item re: not_if condition


#1

Hi folks -

I’m seeing what I think is a bug, but fully willing to acknowledge its actually
my lack of understanding.

I’m trying to setup a cronjob if a node attribute is equal to “0”, and delete
it if its non-zero. I’ve referred to these resource help docs:

http://wiki.opscode.com/display/chef/Resources#Resources-Cron
http://wiki.opscode.com/display/chef/Resources#Resources-ConditionalExecution

I’m running Ruby 1.8.7, and chef-client 10.4…and my recipe looks like this:


cron “logstoragecleanup” do
user "applogshipper"
hour "#{node[‘applogshipper’][‘logstorageserver’][‘hour_to_run’]}"
minute "#{node[‘applogshipper’][‘logstorageserver’][‘minute_to_run’]}"
command "/home/applogshipper/cleanup.sh"
only_if {
node[:applogshipper][:logstorageserver][:enable_cleanup_cronjob].to_i == 0 }
end

#remove existing cron jobs
cron “logstoragecleanup” do
user "applogshipper"
action :delete
not_if {
node[:applogshipper][:logstorageserver][:enable_cleanup_cronjob].to_i == 0 }
end

When I set “enable_cleanup_cronjob” to non-zero, the cron delete action doesn’t
appear to get triggered.

Any suggestions on why this doesn’t work…or maybe a better approach for the
same solution?

Thanks.
Bryan


#2

Hi all, Sorry for the mammoth post!

Obviously I want the same chef code provisioning all my environments, including production, dev, test, CI, etc…

One of the problems I came across is using Vagrant, as it provisions boxes differently to AWS/Xen/Knife bootstrapping. I noticed that Vagrant adds an extra interface card and IP address via host-only-networking to my dev boxes, hence my usual call to node.automatic_attrs[‘ipaddress’] picks up the VirtualBox provisioned one rather than the one I set via Vagrant… frustrating.

For example… I have a bunch of services which need to register themselves with DNS. So I’m setting up an APT/YUM server say, and I want the creation of the server to register apt.mydomain.com or yum.mydomain.com with the local DNS server via dynamic dns. I had this all working on production using node.automatic_attrs[‘ipaddress’]… but with vagrant+virtualbox, this picked up the wrong interface ip address.

I solved this by writing a routine “most_relevant_primary_ip_address”, which resolves to one of any number of interfaces/IP addreses on the local box, and checks them via the ruby-ip gem, as to whether they resolve to the correct provisioning network, and thus using them. This is also useful in amazon and other areas (e.g. IPVS clients) where I have multiple ip addresses going on.

At the moment, I’ve put the code in a library under networking_basic as suggested in the online manual as follows:

networking_basic/libraries/network_utils.rb

require ‘ip’

module NetworkUtils
class Chef::Recipe

def most_relevant_primary_ip_address

…. code

end

end
end

This seems to be the wrong place for it, the function doesn’t really belong to a Chef::Recipe… It needs to be accessible everywhere from each cookbook, and needs to run after ohai has done its stuff, and the node object has been provisioned.

I would appreciate any pointers as to where else I could put this … and also if I’ve just reinvented the wheel because there’s already stuff out there to do this.

Thanks!