0

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)!

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
  • 1
    Why? What reason do you have for a properties file vs the exiting attributes files that are native to Chef? – Tejay Cardon Jul 06 '15 at 21:12
  • An application triggers Jenkins job with all attributes feeded in by the user. It has the version of the core package to be installed in the node. Thus, the recipe file should reflect that version – Anuj Balan Jul 06 '15 at 21:13
  • 1
    And you missed Tejay's point. Why don't you set the version as a chef attribute. That would be native to chef. – Mark O'Connor Jul 07 '15 at 00:12
  • Now this attribute list resides inside the cookbook which is already in the Chef Server right? But how do I set the value to it ? path "#{src_loc}core_feature_{version}.noarch.rpm" Will I be able to pass version to it, set it and then upload to Chef Server? May be my understanding is wrong? – Anuj Balan Jul 07 '15 at 13:18

1 Answers1

0

You have a few options. The best, in my opinion, is to use a json file rather than a properties file. You can then use the -j flag on your chef-client call to read the json file in as normal attributes on your node. The major caveat there is that normal attributes persist from one chef run to the next, so you'd need to be sure they're being re-set with each chef run.

Alternately, you can have jenkins use knife to set the attributes directly on the node. You'll need a plugin knife node attribute set .... OR you could set it on the environment or role, if those are viable options, but they'll involve pulling the environment or role from the server, modifying it, and then pushing it back up.

Another option is to use ruby to read your properties file as part of your attributes/xxxx.rb file. You could then set the node attributes from it at whatever precedent level you want.

attributes/default.rb

properties = IO.read('/path/to/properties/file')
# parse properties and set node attributes like this
node.default[:something] = <your value>
Tejay Cardon
  • 4,193
  • 2
  • 16
  • 31
  • In my case, chef-client run happens via vagrant and it happens once, in order to up the VM. So editing it for each run to feed dynamically generated json file wont work. I was wondering about option 2. We can edit the attribute list on the chef server before vagrant does the chef-client run on the VM? In that case, I can try to add and delete attributes before VM is up and after VM is destroyed. How do I have jenkins manage all this? – Anuj Balan Jul 07 '15 at 13:48
  • But does adding attributes mean editing chef recipe to mention a specific version of some package? (Started experimenting Chef recently) – Anuj Balan Jul 07 '15 at 13:52
  • It depends how you add the attributes. If you have ruby code in your attributes file, then you can make that ruby code act in a dynamic way. – Tejay Cardon Jul 07 '15 at 14:46
  • If you have jenkins calling vagrant, then you could actually have the vagrant file be dynamic. You can then add your attributes directly into the chef provisioner on the vagrant file. – Tejay Cardon Jul 07 '15 at 14:49