Environment path not being applied using ruby_block in recipe


#1

Hi,

I was using ruby_block resource to set an environment variable path to java binary that is installed through yum on linux box as root user. I was able to print the path following the chef-client, but the path “/usr/bin/java” doesn’t get applied to the PATH environment variable. can any one please advise or suggest , where i could be doing it wrong or if ruby_block is not the correct resource to be used in this case. thanks for your time

Expected outcome:

echo $PATH = /sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/java

recipe:

ruby_block 'set-java-path' do block do ENV['PATH'] = "#{ENV['PATH']}:/usr/bin/java" end end puts "System Path is : #{ENV['PATH']}"

System Path is : /sbin:/bin:/usr/sbin:/usr/bin:/opt/chef/embedded/bin:/opt/chef/embedded/bin:/usr/local/sbin:/usr/local/bin

echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin


#2

Your puts will execute before your ruby_block will. The puts happens while the recipe is being processed to build the resource collection and the ruby_block is executed later in the run when the resource collection is being processed.

There is a resource in core chef for natively working with environmental variables though - https://docs.chef.io/resource_env.html

ruby_block 'set-java-path' do
  block do
    ENV['PATH'] = "#{ENV['PATH']}:/usr/bin/java"
  end
end

ruby_block 'print java path' do
  block do
    puts "System Path is : #{ENV['PATH']}"
  end
end

#3

Thank you @Steven_Murawski for correcting me on the ruby_block and I was able to test the “print java path” block. I referred to “resource_env” and it seems to be more applicable for windows environment and in my case I was trying to set the path on redhat 6.7 version. when I log on to the box after the chef-client, I still can’t find the $PATH appended with “/usr/bin/java”.when I check the path, I only observe
env | grep PATH PATH=/sbin:/bin:/usr/sbin:/usr/bin
and for some reason “/usr/bin/java” doesn’t get applied even after the ruby_block executes successfully in my recipe.I was installing the java binary with “yum install java-1.7.0-openjdk-devel” before setting the environment PATH through the ruby_block and after the installation I observed the binary installed to the following path, but it did not get added to $PATH. could you advise how this can be resolved in linux environment, i was able to use bash resource along with `export PATH=$PATH:/usr/bin/java’, but i wouldn’t wanted to use that but prefer other ways to use.thanks for your time

which java
/usr/bin/java


#4

The env resource is a cross-platform resource (and actually requires a bit of help on Windows from the windows_env resource in the windows cookbook). When you just add to ENV in your ruby block, you are modifying it for that process, not updating the system path. To update the system path for everything going forward, use the built in resource.


#5

@Steven_Murawski I am sorry for the delayed response, I have tried to find the built in resource from chef docs and couldn’t identify which resource is ideal for updating system path. could you help me with the resource documentation.Thank you


#6

I think you are looking for https://github.com/chef-cookbooks/windows#windows_path


#7

@Matt_Wrock I believe that was for windows environment.Sorry,I was looking for linux environment in my case


#8

oh so sorry. I saw @Steven_Murawski and knee jerked into windows mode :slight_smile:

Yeah the env resource only has a windows provider. On non windows platforms, you will get UnsupportedAction exceptions. https://github.com/chef/chef/blob/master/lib/chef/provider/env.rb#L143-156


#9

So for us on Ubuntu with custom Ruby installations, what we do is drop a file into /etc/profile.d that adjusts the path. It’s been a while since I’ve worked with RedHat-based systems, though, so I’m not sure if that’d work for you.