Create chef recipe by passing values dynamically, from properties file

chefchef-soloruby

I created a chef recipe which has the installation of a specific rpm mentioned in it. This rpm has a version number which needs to be loaded dynamically from a properties file (which is itself created dynamically). This chef recipe then needs to be loaded into the chef server.

remote_file "Core_feature.rpm" do
path   "#{src_loc}core_feature_v91-2.noarch.rpm"

Here, v91-2 is the value which needs to go in dynamically into the recipe, by reading from a properties file.

Is this achievable? If yes, how do I go on to implement it.(Have no idea on ruby)!

Best Answer

Edit: complete rewrite due to down-voting but without more information I'm not sure what exactly I'm correcting so here is working code that can be copied and pasted.

Assuming contents of properties file placed in /tmp/versions.properties and looks like the following:

apache=2.4.7-1ubuntu4.8
php=5.4.3
sendmail=1.2.3

Chef Cook book

# Copy a file with versions
cookbook_file '/tmp/versions.properties' do
  source 'versions.properties'
  mode '0644'
end

# Setvar
node.default['version'] = ""

# Block where we set the command
ruby_block "set_app_id" do
  block do
     node.set['version'] = "apt-get install apache2=`grep -o 'apache=.*' /tmp/versions.properties | cut -f2- -d'='`"
  end
  action :create
end

# Do a lazy install
execute "install lazy based" do
    command lazy {node[:version]}
end

As a bonus option if you are working strictly on *nix the following works without lazy:

# Execute straight up
 execute "install lazy based" do
    command "apt-get install apache2=`grep -o 'apache=.*' /tmp/versions.properties | cut -f2- -d'='`"
 end

I still used the original premise of the links in the first two posts referenced:

Assuming that properties file is on the server(?) you could use lazy evaluation as described [in this post][1]

Edit: Check this [one out too][2]

[1]: https://stackoverflow.com/questions/26238056/setting-chef-variable-via-a-ruby-block-not-being-executed. [2]: https://stackoverflow.com/questions/20620724/how-to-lazily-evaluate-an-arbitrary-variable-with-chef