- From: Bryan McLellan <
>
- To:
- Subject: [chef] Re: Re: How can I store the output of a bash command as a ruby variable?
- Date: Thu, 29 Sep 2011 08:19:46 -0400
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
- [chef] Listing files in cookbook/files/default/, Jason J. W. Williams, 09/24/2011
- [chef] Re: Listing files in cookbook/files/default/, Denis Barishev, 09/25/2011
- [chef] Re: Listing files in cookbook/files/default/, Joshua Timberman, 09/25/2011
- [chef] Re: Re: Listing files in cookbook/files/default/, Jason J. W. Williams, 09/25/2011
- [chef] Re: Re: Listing files in cookbook/files/default/, Jason J. W. Williams, 09/27/2011
- [chef] Re: Re: Re: Listing files in cookbook/files/default/, Adam Jacob, 09/27/2011
- [chef] Re: Re: Re: Re: Listing files in cookbook/files/default/, Jason J. W. Williams, 09/27/2011
- [chef] Re: Re: Re: Re: Re: Listing files in cookbook/files/default/, Adam Jacob, 09/27/2011
- [chef] How can I store the output of a bash command as a ruby variable?, Geoff Meakin Acid, 09/29/2011
- [chef] Re: How can I store the output of a bash command as a ruby variable?, Akzhan Abdulin, 09/29/2011
- [chef] Re: Re: How can I store the output of a bash command as a ruby variable?, Bryan McLellan, 09/29/2011
- [chef] Knifing multiple different chef servers, Geoff Meakin Acid, 09/29/2011
- [chef] Re: Knifing multiple different chef servers, Bryan Brandau, 09/29/2011
- [chef] Re: Re: Knifing multiple different chef servers, KC Braunschweig, 09/30/2011
- [chef] Re: Re: Re: Knifing multiple different chef servers, Joshua Timberman, 09/30/2011
- [chef] Re: How can I store the output of a bash command as a ruby variable?, Geoff Meakin Acid, 09/29/2011
Archive powered by MHonArc 2.6.16.