# How to use Ohai in a EC2 userdata script?

**URL:** <https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069>\
**Category:** Chef Infra (archive)\
**Created:** [September 28, 2011, 3:15am UTC](https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069 "2011-09-28T03:15:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hedgehog](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/hedgehog/32/620_2.png) [@hedgehog](https://discourse.chef.io/u/hedgehog)\
**Post date:** [September 28, 2011, 3:15am UTC](https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069/1 "2011-09-28T03:15:15Z")

</div>

Hi,  
I am using Ohai 0.6.4.  
I don’t seem to be able to get Ohai to return EC2 data consistently.  
When I run this ruby script by passing it in via the AMI userdata:

require 'ohai’  
o = Ohai::System.new  
o.all\_plugins  
o.require\_plugin(‘ec2’)  
$stderr.puts o.fetch\_metadata  
$stderr.puts o.looks\_like\_ec2?  
$stderr.puts o[:ec2].inspect

I get the results:

\<ec2/ami-data-hash\>  
false  
nil

So, the metadata is retrieved, but the second two EC2 methods don’t  
seem to work.  
More puzzling is that if I SSH to the instance, and run this same  
script, the o[:ec2] contains the data I’d expect.

So how does one use Ohai, correctly, in an EC2 user data script?

Appreciate any insights.

–  
πόλλ’ οἶδ ἀλώπηξ, ἀλλ’ ἐχῖνος ἓν μέγα  
[The fox knows many things, but the hedgehog knows one big thing.]  
Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
[http://hedgehogshiatus.com](http://hedgehogshiatus.com)

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [September 28, 2011, 3:18am UTC](https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069/2 "2011-09-28T03:18:50Z")

</div>

It's broken, gem install ohai --pre

–AJ

On 28 September 2011 16:15, Hedge Hog [hedgehogshiatus@gmail.com](mailto:hedgehogshiatus@gmail.com) wrote:

> Hi,  
> I am using Ohai 0.6.4.  
> I don't seem to be able to get Ohai to return EC2 data consistently.  
> When I run this ruby script by passing it in via the AMI userdata:
> 
> require 'ohai'  
> o = Ohai::System.new  
> o.all\_plugins  
> o.require\_plugin('ec2')  
> $stderr.puts o.fetch\_metadata  
> $stderr.puts o.looks\_like\_ec2?  
> $stderr.puts o[:ec2].inspect
> 
> I get the results:
> 
> \<ec2/ami-data-hash\>  
> false  
> nil
> 
> So, the metadata is retrieved, but the second two EC2 methods don't  
> seem to work.  
> More puzzling is that if I SSH to the instance, and run this same  
> script, the o[:ec2] contains the data I'd expect.
> 
> So how does one use Ohai, correctly, in an EC2 user data script?
> 
> Appreciate any insights.
> 
> --  
> πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα  
> [The fox knows many things, but the hedgehog knows one big thing.]  
> Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
> [http://hedgehogshiatus.com](http://hedgehogshiatus.com)

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [September 28, 2011, 3:19am UTC](https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069/3 "2011-09-28T03:19:30Z")

</div>

..run with -l debug to see the StringIO exception being bubbled up  
from popen or whatever the fuck it was, already fixed in the RC.

–AJ

On 28 September 2011 16:18, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> It's broken, gem install ohai --pre
> 
> –AJ
> 
> On 28 September 2011 16:15, Hedge Hog [hedgehogshiatus@gmail.com](mailto:hedgehogshiatus@gmail.com) wrote:
> 
> > Hi,  
> > I am using Ohai 0.6.4.  
> > I don't seem to be able to get Ohai to return EC2 data consistently.  
> > When I run this ruby script by passing it in via the AMI userdata:
> > 
> > require 'ohai'  
> > o = Ohai::System.new  
> > o.all\_plugins  
> > o.require\_plugin('ec2')  
> > $stderr.puts o.fetch\_metadata  
> > $stderr.puts o.looks\_like\_ec2?  
> > $stderr.puts o[:ec2].inspect
> > 
> > I get the results:
> > 
> > \<ec2/ami-data-hash\>  
> > false  
> > nil
> > 
> > So, the metadata is retrieved, but the second two EC2 methods don't  
> > seem to work.  
> > More puzzling is that if I SSH to the instance, and run this same  
> > script, the o[:ec2] contains the data I'd expect.
> > 
> > So how does one use Ohai, correctly, in an EC2 user data script?
> > 
> > Appreciate any insights.
> > 
> > --  
> > πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα  
> > [The fox knows many things, but the hedgehog knows one big thing.]  
> > Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
> > [http://hedgehogshiatus.com](http://hedgehogshiatus.com)

---

<div class="post-metadata">

**Author:** ![hedgehog](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/hedgehog/32/620_2.png) [@hedgehog](https://discourse.chef.io/u/hedgehog)\
**Post date:** [September 28, 2011, 3:39am UTC](https://discourse.chef.io/t/how-to-use-ohai-in-a-ec2-userdata-script/2069/4 "2011-09-28T03:39:03Z")

</div>

On Wed, Sep 28, 2011 at 1:18 PM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> It's broken, gem install ohai --pre

Okay, thanks

> –AJ
> 
> On 28 September 2011 16:15, Hedge Hog [hedgehogshiatus@gmail.com](mailto:hedgehogshiatus@gmail.com) wrote:
> 
> > Hi,  
> > I am using Ohai 0.6.4.  
> > I don't seem to be able to get Ohai to return EC2 data consistently.  
> > When I run this ruby script by passing it in via the AMI userdata:
> > 
> > require 'ohai'  
> > o = Ohai::System.new  
> > o.all\_plugins  
> > o.require\_plugin('ec2')  
> > $stderr.puts o.fetch\_metadata  
> > $stderr.puts o.looks\_like\_ec2?  
> > $stderr.puts o[:ec2].inspect
> > 
> > I get the results:
> > 
> > \<ec2/ami-data-hash\>  
> > false  
> > nil
> > 
> > So, the metadata is retrieved, but the second two EC2 methods don't  
> > seem to work.  
> > More puzzling is that if I SSH to the instance, and run this same  
> > script, the o[:ec2] contains the data I'd expect.
> > 
> > So how does one use Ohai, correctly, in an EC2 user data script?
> > 
> > Appreciate any insights.
> > 
> > --  
> > πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα  
> > [The fox knows many things, but the hedgehog knows one big thing.]  
> > Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
> > [http://hedgehogshiatus.com](http://hedgehogshiatus.com)

--  
πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα  
[The fox knows many things, but the hedgehog knows one big thing.]  
Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
[http://hedgehogshiatus.com](http://hedgehogshiatus.com)
