Reputation: 361
I am declaring a variable in Chef/Ruby
and assigning it a value. The value is different for different environments. I am setting the value in respective environment files like this:
region = us-west-1
In the template file I am using it this way
region =<%= @region %>
and in the recipe as
:region =>node.region
The instance doesn't come up when my fix is merged. Is this right way of doing it or am I missing something?
Upvotes: 1
Views: 546
Reputation: 318
Here are two of my favourite ways to do that:
Define the default for the attribute in an attribute file. so in <cookbook_name>/attributes/default.rb
file add this line:
default['instance_region'] = 'us-west-1'
and then in your recipe where you are adding the template:
variables(region: node['instance_region'])
you can access that in you template as you mentioned:
region =<%= @region %>
For wider usage you can define such value within a chef library. so in <cookbook>/libraries/common.rb
add:
module Common
def instance_region
# This will return the name of AWS region that the nodes is in.
shell_out!('curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/').stdout
end
end
and then in your recipe you can use it just by calling plain instance_region
Upvotes: 1