Chef Ohai: how to use the newly installed Ruby with gem_package

Using chef I have a simple recipe that sets up a gem, for example:

 gem_package "passenger" do version node['passenger']['version'] end 

I also want to install ruby ​​with another cookbook, it could be Ruby 1.9.3 for some servers and Ruby Enterprise 1.8.7 for others. So I decided to use gem_binary and ohai for this, for example:

 gem_package "passenger" do version node['passenger']['version'] gem_binary "#{languages['ruby']['bin_dir']/gem}" end 

But then the problems begin, because languages['ruby'] does not change when a new ruby ​​is installed. Ruby Enterprise installs in /opt/ruby-enterprise and adds itself to PATH through /etc/profile.d/ree.sh , but does not get it for ohai during the same run, but gets it in the next run.

The first launch of ohai says that languages['ruby'] set to /opt/vagrant_ruby/bin/ruby when used with vagrant and chef_solo . And the passenger pearl is set in the wrong ruby.

How can I make ohai recognize a newly installed ruby?

+6
source share
2 answers

I usually work with RVM, which has the same problem. There, I usually hardcode the path to the gem binary and leave the last bit as an attribute.

eg.

sort of

  5 node["rvm"]["rubies"].each do |ruby| 6 gem_package "[#{ruby}]-passenger" do 7 package_name "passenger" 8 version node[:passenger][:version] 9 gem_binary "/usr/local/rvm/bin/gem-#{ruby}" 10 options "--no-ri --no-rdoc" 11 end 12 end 

As an alternative, we used bash blocks and got the corresponding file. Note that when using bash blocks, only the last value in the block will be used to determine success, it is often advisable to link them with &&

+1
source

I think there is a way to reload ohai attributes while running the chef's recipe:

You need to use the ohai resource:

 ohai "reload" do action :reload end 

More details here: http://wiki.opscode.com/display/chef/Resources#Resources-Ohai

+1
source

Source: https://habr.com/ru/post/919602/


All Articles