Create chef recipe by passing values dynamically, from properties file


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/ and looks like the following:


Chef Cook book

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

# 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/ | cut -f2- -d'='`"
  action :create

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

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/ | cut -f2- -d'='`"

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]: [2]: