concept47
concept47

Reputation: 31736

how to make sure a chef script block doesn't run every time you run chef-solo

Totally new to chef ... We used to install elasticsearch from a ppa repo but now I want to build it from scratch (repo isn't being updated any more) each time I change the version number in my attributes/default.rb

Right now I have a bash block that just pulls down the gz file, unzips it and links it to the right place, but I don't know how to make it so that it won't run everytime I run chef-solo.

Any suggestions?

Upvotes: 2

Views: 765

Answers (1)

Draco Ater
Draco Ater

Reputation: 21226

I create a text file with version inside. The logic is simple.

  • If the file does not exist: unzip.
  • If the file exists and the version (checked with IO.read(filename)) is different: unzip.
  • If the file exists and the version is the same: do nothing.

By default the resource that creates file has action :nothing and is notified by unzip. This way not only unzip will not run, if the version is the same, but the version file will be left untouched too.

Pseudo-code to illustrate the logic:

unzip "resource_name" do
  not_if { ::File.exists?( filename ) and node[:version]==IO.read( filename ).strip }
  notifies :create, "file[#{filename}]"
end

file filename do
  action :nothing
  content node[:version]
end

Upvotes: 3

Related Questions