简体   繁体   中英

How do I use the only_if for regions in EC2 instances

I'm trying to setup a only_if recipe, I only want file1 to be used in our East region and File2 to be using in the West region. The results right now is, the client bypasses the cookbook (file) because of the only_if. I looked at the logs, but they are not very informative. So I'm at a lost as to why its not working.

I have also tried this way with the same negative results.
node[ec2][placement_availability_zone]

cookbook_file "/dir/dir/dir/file.json" do
only_if { node['ec2']['region'] == "us-east-1"}
  source "dir/file1.json"
  owner "user"
  group "group"
  action :create
  notifies :restart, "service[app-client]", :delayed
end

cookbook_file "/etc/dir/dir/file.json" do
only_if { node['ec2']['region'] == "us-west-2"}
  source "dir/file2.json"
  owner "user"
  group "group"
  action :create
  notifies :restart, "service[app-client]", :delayed
end

Here is the ohai results on the same server.

ohai | grep region
  "dm_region_hash": {
  "json?": "{\n  \"devpayProductCodes\" : null,\n  \"privateIp\" :        \”0.0.0.0\”,\n  \"availabilityZone\" : \"us-west-2c\",\n  \"version\" : \”date\”,\n  \"region\" : \"us-west-2\",\n  \"instanceId\" : \”ID\”,\n  \"billingProducts\" : null,\n  \"instanceType\" : \"t2.micro\",\n  \"accountId\" : \"\",\n  \"kernelId\" : null,\n  \"ramdiskId\" : null,\n  \"architecture\" : \"x86_64\",\n  \"imageId\" : \”ami-111111\”,\n  \"pendingTime\" : \”date\time\”\n}”,

So for reasons that are entirely beyond me, you can't actually get the region via the EC2 metadata system. You can get the availability zone, and fortunately the region is always just the AZ name with the last character chopped off:

node['ec2']['placement_availability_zone'].chop

Just hope they never make a region with more than 26 AZs and that should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM