Capture output from a command or read contents from a file

What’s the proper way to read output from a command or contents from a file
to use in a variable (to pass to a template for example) in a chef recipe?
Or is this bad form in some way?

I couldn’t figure out how to do it with a built in resource so I gave up
and went to some ruby … but weird things happen. It seems to execute the
ruby code in the wrong place (too early … before figlet is installed).
I’m probably doesn’t something wrong, however.

Any suggestions?

installs the figlet RPM

include_recipe “figlet”

banner = ''
io = IO.popen("/usr/bin/figlet -w 160 #{node.name}")
io.each do |line|
banner += line
end

template “/etc/motd” do
source "motd.erb"
mode 0644
variables(
:banner => banner
)
end

This is an interesting case. I wrote a step yesterday to write the mdadm configuration file and used the following:

execute "mdadm_detail" do
  command "mdadm --detail --scan >> /etc/mdadm.conf"
  not_if "cat /etc/mdadm.conf | grep ARRAY"
end

Outside of capturing the mdadm --detail --scan output using Ruby and passing it to mdadm.conf through template variables, do people have alternate approaches?

--
Hector

On Jan 17, 2012, at 4:05 PM, Harlan Barnes wrote:

What's the proper way to read output from a command or contents from a file to use in a variable (to pass to a template for example) in a chef recipe? Or is this bad form in some way?

I couldn't figure out how to do it with a built in resource so I gave up and went to some ruby ... but weird things happen. It seems to execute the ruby code in the wrong place (too early ... before figlet is installed). I'm probably doesn't something wrong, however.

Any suggestions?

installs the figlet RPM

include_recipe "figlet"

banner = ''
io = IO.popen("/usr/bin/figlet -w 160 #{node.name}")
io.each do |line|
banner += line
end

template "/etc/motd" do
source "motd.erb"
mode 0644
variables(
:banner => banner
)
end

Take a look at using Mixlib::ShellOut (previously Chef::ShellOut).

Here's an outdated example (needs to be updated to Mixlib from Chef).
https://github.com/opscode/cookbooks/blob/master/drbd/recipes/pair.rb#L49

Thanks,
Matt Ray
Senior Technical Evangelist | Opscode Inc.
matt@opscode.com | (512) 731-2218
Twitter, IRC, GitHub: mattray

On Tue, Jan 17, 2012 at 3:26 PM, Hector Castro hectcastro@gmail.com wrote:

This is an interesting case. I wrote a step yesterday to write the mdadm configuration file and used the following:

   execute "mdadm_detail" do
     command "mdadm --detail --scan >> /etc/mdadm.conf"
     not_if "cat /etc/mdadm.conf | grep ARRAY"
   end

Outside of capturing the mdadm --detail --scan output using Ruby and passing it to mdadm.conf through template variables, do people have alternate approaches?

--
Hector

On Jan 17, 2012, at 4:05 PM, Harlan Barnes wrote:

What's the proper way to read output from a command or contents from a file to use in a variable (to pass to a template for example) in a chef recipe? Or is this bad form in some way?

I couldn't figure out how to do it with a built in resource so I gave up and went to some ruby ... but weird things happen. It seems to execute the ruby code in the wrong place (too early ... before figlet is installed). I'm probably doesn't something wrong, however.

Any suggestions?

installs the figlet RPM

include_recipe "figlet"

banner = ''
io = IO.popen("/usr/bin/figlet -w 160 #{node.name}")
io.each do |line|
banner += line
end

template "/etc/motd" do
source "motd.erb"
mode 0644
variables(
:banner => banner
)
end

On Tue, Jan 17, 2012 at 4:44 PM, Matt Ray matt@opscode.com wrote:

Take a look at using Mixlib::ShellOut (previously Chef::ShellOut).
GitHub - chef/mixlib-shellout: mixin library for subprocess management, output collection

Here's an outdated example (needs to be updated to Mixlib from Chef).
https://github.com/opscode/cookbooks/blob/master/drbd/recipes/pair.rb#L49

Thanks Matt. This is probably a stupid question, but Is Mixlib::ShellOut
packaged with Chef? I don't see it in the latest version and gem install
mixlib-shellout fails ("not in any repository"). I just wanted to make sure
before I download and install it from github.

On Tuesday, January 17, 2012 at 2:39 PM, Harlan Barnes wrote:

On Tue, Jan 17, 2012 at 4:44 PM, Matt Ray <matt@opscode.com (mailto:matt@opscode.com)> wrote:

Take a look at using Mixlib::ShellOut (previously Chef::ShellOut).
GitHub - chef/mixlib-shellout: mixin library for subprocess management, output collection

Here's an outdated example (needs to be updated to Mixlib from Chef).
https://github.com/opscode/cookbooks/blob/master/drbd/recipes/pair.rb#L49

Thanks Matt. This is probably a stupid question, but Is Mixlib::ShellOut packaged with Chef? I don't see it in the latest version and gem install mixlib-shellout fails ("not in any repository"). I just wanted to make sure before I download and install it from github.
It's only a prerelease right now, wanted to wait to make it "final."

Anyway, using Chef::ShellOut should be fine, and compatibility should be maintained even when we switch to using the mixlib version. The current plan is to introduce deprecation warnings for Chef::ShellOut starting with Chef 0.11.

--
Dan DeLeo