Question

I'm trying to use the IP of an instance on AWS OpsWorks in a Chef recipe, code looks like this:

variables(
  :db_host => (node[:scraper][:db_host] rescue nil),
  :db_user => (node[:scraper][:db_user] rescue nil),
  :db_pass => (node[:scraper][:db_pass] rescue nil),
  :db_name => (node[:scraper][:db_name] rescue nil),
  :beanstalk_host => (node[:opsworks][:layers][:admin][:instances].first[:private_ip])
)

But I get a TypeError - Symbol as array index error.

I'm not a Ruby developer, so I'm a bit lost with what to try. Even the docs list that syntax:

The following example obtains the private IP address of the HAProxy layer's first instance: node[:opsworks][:layers][:lb][:instances].first[:private_ip]

When I log the value of node[:opsworks][:layers][:admin][:instances].first I get:

["admin1", {"elastic_ip"=>"54.221.245....

Was it helpful?

Solution

The error is correct: you can't use a symbol as an array index.

By the output of you last line, since it starts with a [, it is an array.

Most likely you are trying to index the dictionary on the second position of the array with the symbol :private_ip. In this case, the correct line is

node[:opsworks][:layers][:admin][:instances].first[1][:private_ip]

You need to go step by step in your mind. If node[:opsworks][:layers][:admin][:instances].first returns

["admin1", {"elastic_ip"=>"54.221.245....}]

Then node[:opsworks][:layers][:admin][:instances].first[1] will return

{"elastic_ip"=>"54.221.245....}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top