Question

I used chef for automation.

In one of my scenarios I need to extract a version number of passenger from rvm installed in my system.

I used 'pasver' variable for this:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'    

But If I use this variable, in this case:

if ! ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver}/buildout/apache2/mod_passenger.so") then

Then this condition is always false, and code in if block does not run.

This is because pasver variable contains new of line.

How I can remove this new of line ?

I used next way, but without successfully:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.gsub("\n",'')

Was it helpful?

Solution 4

  1. Chef-users mailing list suggested to me a solution for regexp

    pasver = /usr/local/rvm/bin/gem list | grep passenger.sub /.((.)).*/m, '\1'

  2. But It is not all, there was a problem with using this code in erb template itself. Variable value disappears, but I don'k know why.

For example this code isn't worked.

 <% pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.chomp %>
    PassengerRoot <%= "/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}" %>
    PassengerDefaultRuby /usr/local/rvm/wrappers/<%= node['redmine']['rubyversion']%>/ruby

This file generates:

    LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19
    /buildout/apache2/mod_passenger.so

I don't get strange behaviour in IRB never. But chef runtime is something else.

My solution is using variables in recipe and then pass it to template resource:

template ".." do
variables ({
:pasver => pasver
})
end

OTHER TIPS

Why not use

pasver.chomp!

after the system call or,

if ! ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp}/buildout/apache2/mod_passenger.so") then

How about this:

irb> require 'rubygems'
irb> g = Gem::Specification.find_all_by_name 'passenger'
irb> g.first.version.version
=> "4.0.19"

You can use rstrip() on any string to remove trailing whitespace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top