0

I am a new to Chef and I am trying to test AWS cluster configuration for 2 db serves + 2 application servers using kitchen-ec2 and facing the following problem:

I cannot dynamically pass ip_address_1 from recipe#1 (recipe#1 for server1 is completed) to recipe#2 (which is config recipe for server2 and is being executed). It would be helpful to hear your advice.

One of the things I can do is to explicitly set IP address in .kitchen.yml for each box I am trying to configure , but I am curious if there is any way I can do it on the fly, smth like Ohai node['ipaddress'] attribute but more generic, maybe a list of all server IP addresses configured in my cluster.

recipe#1 - done - server1 is configured, AWS assigned private ipaddress1
recipe#2 - in progress - server2 configuration is being applied but it requires ipaddress1

Thank you in advance,
Dmitry

Dmitry S
  • 4,990
  • 2
  • 24
  • 32

1 Answers1

0

I'm not sure if this is what you're looking for but you can access all servers configuration in your recipe via search, e.g.:

search(:node, 'recipes:"recipe#2"')

And then iterate over results to access nodes configuration.

An example, used for populating /etc/hosts. I'm using knife-ec2, I'm not sure if kitchen-ec2 provides all these attributes.

recipes/default.rb

template "/etc/hosts" do
  source "etc/hosts.erb"
  owner "root"
  group "root"
  mode "0644"
  variables(:servers => search(:node, "chef_environment:#{node.chef_environment}"),
            :region => node['ec2']['placement_availability_zone'].match(/(\w*\-\w*\-\d*).*/)[1])
end

templates/default/etc/hosts.erb

<% @servers.each do |n| %>
   <% if n["ec2"] %>
      <% region = n["ec2"]["placement_availability_zone"].match(/(\w*\-\w*\-\d*).*/)[1] %>
      <% if region == @region && n["ec2"]["local_ipv4"] %>
<%= n["ec2"]["local_ipv4"] -%> <%= n.name -%> <%= n["ec2"]["hostname"] -%>

      <% elsif n["ec2"]["public_ipv4"] %>
<%= n["ec2"]["public_ipv4"] -%> <%= n.name -%> <%= n["ec2"]["public_hostname"] -%>

      <% end %>
   <% end %>
<% end %>
makhan
  • 3,809
  • 2
  • 18
  • 24