[chef] Re: How can I store the output of a bash command as a ruby variable?


Chronological Thread 
  • From: Geoff Meakin Acid < >
  • To:
  • Subject: [chef] Re: How can I store the output of a bash command as a ruby variable?
  • Date: Thu, 29 Sep 2011 13:35:55 +0100

Thanks guys, thats perfect and a real help

--

Geoff



On 29 Sep 2011, at 13:19, Bryan McLellan wrote:

On Thu, Sep 29, 2011 at 5:44 AM, Akzhan Abdulin
< "> > wrote:
v = `hostname -d`
but you should look at ohai attributes before.

Right. Akzhan's example is using backticks, which is the simple Ruby
way to execute a command and grab its output. Ohai and Chef use more
complicated methods for running commands to deal with numerous edge
cases, like where STDOUT and STDERR go, and what happens if the
command blocks on something and hangs.

You can get all the hostname information from ohai. Hostname, domain,
and fqdn are all stored in top level node attributes. So in a recipe
you could have:

---
h = node[:hostname]
d = node[:domain]
f = node[:fqdn]

Chef::Log.info "My hostname is #{h}, my domain is #{d}, and my FQDN is #{f}"
---

These are automatic attributes [1] populated on each run by Ohai data.

If you want to use them in a template you can do:

---
template "/tmp/foo" do
 source "foo.erb"
 variables({
   :h => node[:hostname],
   :d => node[:domain]
 })
end
---

Then you will have an h and d variable available to you in the
template that will automatically be populated with the data from Ohai.
This is important because if you ever decided that you needed to get
this variable a different way, you don't have to change your template
but just change the above template resource so that 'd' or 'h' is
populated from somewhere else.

Bryan

[1] http://wiki.opscode.com/display/chef/Automatic+Attributes




Archive powered by MHonArc 2.6.16.

§