How ---- in a pain free method - to get the public ip address on ec2

Hi,

I am having a very hard time getting the public ip address on ec2

  1. the first recipes creates these resources

directory “/etc/chef/ohai/hints” do
owner "root"
group "root"
recursive true
action :create
end

file “/etc/chef/ohai/hints/ec2.json” do
owner "root"
group "root"
action :create
end

  1. In another recipe later down the line I get the ip address
    if File.exists?("/etc/chef/ohai/hints/ec2.json")
    ipaddress = node[‘ec2’][‘public_ipv4’]
    file “/tmp/public” do
    owner "root"
    group "root"
    mode "0755"
    content "#{ipaddress}"
    action :create
    end
    end

  2. Wowee…the file is not created. Why does this happen?

  3. And if I use this instead I get the below without checking for the
    existince of /etc/chef/ohai/hints/ec2.json

    ipaddress = node[‘ec2’][‘public_ipv4’]
    file “/tmp/public” do
    owner "root"
    group "root"
    mode "0755"
    content "#{ipaddress}"
    action :create
    end

================================================================================
Recipe Compile Error in
/var/chef/cache/cookbooks/cloudera/recipes/hostname.rb

NoMethodError

undefined method `[]’ for nil:NilClass

Cookbook Trace:

/var/chef/cache/cookbooks/cloudera/recipes/hostname.rb:17:in `from_file’

Relevant File Content:

/var/chef/cache/cookbooks/cloudera/recipes/hostname.rb:

17>> ipaddress = node[‘ec2’][‘public_ipv4’]
18: file “/tmp/public” do
19: owner "root"
20: group "root"
21: mode "0755"
22: content "#{ipaddress}"
23: action :create
24: end

[2014-08-13T01:51:21+00:00] ERROR: Running exception handlers
[2014-08-13T01:51:21+00:00] ERROR: Creating JSON exception report
[2014-08-13T01:51:21+00:00] ERROR: Exception handlers complete

On Tuesday, August 12, 2014 at 6:55 PM, David Montgomery wrote:

Hi,

I am having a very hard time getting the public ip address on ec2

  1. the first recipes creates these resources

directory "/etc/chef/ohai/hints" do
owner "root"
group "root"
recursive true
action :create
end

file "/etc/chef/ohai/hints/ec2.json" do
owner "root"
group "root"
action :create
end

  1. In another recipe later down the line I get the ip address
    if File.exists?("/etc/chef/ohai/hints/ec2.json")
    ipaddress = node['ec2']['public_ipv4']
    file "/tmp/public" do
    owner "root"
    group "root"
    mode "0755"
    content "#{ipaddress}"
    action :create
    end
    end

  2. Wowee.....the file is not created. Why does this happen?

  3. And if I use this instead I get the below without checking for the existince of /etc/chef/ohai/hints/ec2.json

ipaddress = node['ec2']['public_ipv4']
file "/tmp/public" do
owner "root"
group "root"
mode "0755"
content "#{ipaddress}"
action :create
end

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/cloudera/recipes/hostname.rb

NoMethodError

undefined method `' for nil:NilClass

Cookbook Trace:

/var/chef/cache/cookbooks/cloudera/recipes/hostname.rb:17:in `from_file'

Relevant File Content:

/var/chef/cache/cookbooks/cloudera/recipes/hostname.rb:

17>> ipaddress = node['ec2']['public_ipv4']
18: file "/tmp/public" do
19: owner "root"
20: group "root"
21: mode "0755"
22: content "#{ipaddress}"
23: action :create
24: end

[2014-08-13T01:51:21+00:00] ERROR: Running exception handlers
[2014-08-13T01:51:21+00:00] ERROR: Creating JSON exception report
[2014-08-13T01:51:21+00:00] ERROR: Exception handlers complete

Two things.

  1. Ohai runs before any of your chef code is executed. If you change any of Ohai’s configuration (like adding hints), you need to re-run ohai (you can do this with the ohai resource)

  2. Any file you create with the file resource isn’t created until the converge phase (which happens after chef has evaluated all of your recipes).

By far the easiest way to go is to just do everything out of band, e.g. touch the hint file before you run chef. Otherwise you’re gonna need to use run_action hackery to make chef converge resources during the compile phase.

--
Daniel DeLeo