Run ruby script during chef-client run?


#1

Hi All :slight_smile:

I am trying to run a ruby script with /opt/chef-server/embedded/bin/ruby
interpreter during chef-client run.

This works:

execute ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

These do not work:

ruby ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

and

ruby ‘process_something’ do
action :run
command '/usr/local/bin/process_something.rb’
end

I run the cookbook with ‘-l debug’ but eventually got only:

[2015-03-19T14:24:41+00:00] INFO: ruby[process_something] ran successfully

in the logs.

Can anyone tell me how to run a ruby script during chef-client run with
interpreter from /opt/chef-server/embedded/bin/ruby or
/opt/chef/embedded/bin/ruby?

I know that I can use ruby_code resource but I am under impression that it
should be used rather for blocks of ruby code and not for scripts, is it so?

BR,
Rafal.


#2

Rafal:

So yes, the ruby_block resource is for an inline block of Ruby code you want to run within the context of the Chef execution.

As for your execute resource, that appears to a way to do it. Curious though; is there any reason you wouldn’t write your script as a library for your cookbook, and then use the ruby_block resource to execute it? Do you need the script outside of the scope of Chef convergences?

–
Jeff Byrnes
@thejeffbyrnes
Lead DevOps Engineer
EverTrue
704.516.4628

On March 19, 2015 at 10:27:54 AM, Rafał Radecki (radecki.rafal@gmail.com) wrote:

Hi All :slight_smile:

I am trying to run a ruby script with /opt/chef-server/embedded/bin/ruby interpreter during chef-client run.

This works:

execute ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby /usr/local/bin/process_something.rb’
end

These do not work:

ruby ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby /usr/local/bin/process_something.rb’
end

and

ruby ‘process_something’ do
action :run
command '/usr/local/bin/process_something.rb’
end

I run the cookbook with ‘-l debug’ but eventually got only:

[2015-03-19T14:24:41+00:00] INFO: ruby[process_something] ran successfully

in the logs.

Can anyone tell me how to run a ruby script during chef-client run with interpreter from /opt/chef-server/embedded/bin/ruby or /opt/chef/embedded/bin/ruby?

I know that I can use ruby_code resource but I am under impression that it should be used rather for blocks of ruby code and not for scripts, is it so?

BR,
Rafal.


#3

I like rather to have the script outside of chef, it is easier to perform
troubleshooting (from my point of view) then and also in case of change in
automation solution (for example a move to puppet) smaller amount of
changes is needed to move the script to the new environment :wink:

BR,
Rafal.

2015-03-19 16:10 GMT+01:00 Jeff Byrnes jeff@evertrue.com:

Rafal:

So yes, the ruby_block resource is for an inline block of Ruby code you
want to run within the context of the Chef execution.

As for your execute resource, that appears to a way to do it. Curious
though; is there any reason you wouldn’t write your script as a library for
your cookbook, and then use the ruby_block resource to execute it? Do you
need the script outside of the scope of Chef convergences?

–
Jeff Byrnes
@thejeffbyrnes http://twitter.com/thejeffbyrnes
Lead DevOps Engineer
EverTrue http://www.evertrue.com/
704.516.4628

On March 19, 2015 at 10:27:54 AM, Rafał Radecki (radecki.rafal@gmail.com)
wrote:

Hi All :slight_smile:

I am trying to run a ruby script with /opt/chef-server/embedded/bin/ruby
interpreter during chef-client run.

This works:

execute ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

These do not work:

ruby ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

and

ruby ‘process_something’ do
action :run
command '/usr/local/bin/process_something.rb’
end

I run the cookbook with ‘-l debug’ but eventually got only:

[2015-03-19T14:24:41+00:00] INFO: ruby[process_something] ran successfully

in the logs.

Can anyone tell me how to run a ruby script during chef-client run with
interpreter from /opt/chef-server/embedded/bin/ruby or
/opt/chef/embedded/bin/ruby?

I know that I can use ruby_code resource but I am under impression that it
should be used rather for blocks of ruby code and not for scripts, is it so?

BR,
Rafal.


#4

this is as expected. ruby_block runs raw ruby code specified via the code
attribute. For arbitrary script, use the execute resources, given you are
specifying the ruby interpreter, its just any other command.

On Thu, Mar 19, 2015 at 8:16 AM, Rafał Radecki radecki.rafal@gmail.com
wrote:

I like rather to have the script outside of chef, it is easier to perform
troubleshooting (from my point of view) then and also in case of change in
automation solution (for example a move to puppet) smaller amount of
changes is needed to move the script to the new environment :wink:

BR,
Rafal.

2015-03-19 16:10 GMT+01:00 Jeff Byrnes jeff@evertrue.com:

Rafal:

So yes, the ruby_block resource is for an inline block of Ruby code you
want to run within the context of the Chef execution.

As for your execute resource, that appears to a way to do it. Curious
though; is there any reason you wouldn’t write your script as a library for
your cookbook, and then use the ruby_block resource to execute it? Do you
need the script outside of the scope of Chef convergences?

–
Jeff Byrnes
@thejeffbyrnes http://twitter.com/thejeffbyrnes
Lead DevOps Engineer
EverTrue http://www.evertrue.com/
704.516.4628

On March 19, 2015 at 10:27:54 AM, Rafał Radecki (radecki.rafal@gmail.com)
wrote:

Hi All :slight_smile:

I am trying to run a ruby script with /opt/chef-server/embedded/bin/ruby
interpreter during chef-client run.

This works:

execute ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

These do not work:

ruby ‘process_something’ do
action :run
command '/opt/chef-server/embedded/bin/ruby
/usr/local/bin/process_something.rb’
end

and

ruby ‘process_something’ do
action :run
command '/usr/local/bin/process_something.rb’
end

I run the cookbook with ‘-l debug’ but eventually got only:

[2015-03-19T14:24:41+00:00] INFO: ruby[process_something] ran successfully

in the logs.

Can anyone tell me how to run a ruby script during chef-client run with
interpreter from /opt/chef-server/embedded/bin/ruby or
/opt/chef/embedded/bin/ruby?

I know that I can use ruby_code resource but I am under impression that
it should be used rather for blocks of ruby code and not for scripts, is it
so?

BR,
Rafal.