Dual-Cloud capable chef cookbook

Currently we have a AWS environment that we stand up using Chef. However, we are now looking at adding Azure to the mix to be considered multi-cloud capable for our application teams. Since the original cookbooks were designed for AWS deployment, is there a way inside the cookbook to understand what cloud provider environment its being deployed in (i.e. AWS vs Azure) then switch to the correct attributes type for things like S3 buckets for AWS or Blob for Azure?

env = {
'dev' => 'shared-dev',
'stage' => shared-stage',
'prod' => 'shared-prod',
}

AWS
default['application']['s3-bucket'] = "https://s3.amazonaws.com/#{env[node.chef_environment]}/application-#{node.chef_environment}"

If/else statement to change s3-bucket name if deployed in the Azure cloud environment instead...

Azure
default['application']['s3-bucket'] = "StorageAccount.blob.core.windows.net/#{env[node.chef_environment]}/application-#{node.chef_environment}"

Howdy!

Ohai should be pulling in some data about the cloud environment for you, under node['cloud'].

You'll find the platform provider at node['cloud']['provider'], and should come up as 'ec2' or 'azure' for those clouds. If you run ohai from the command line (and probably redirect it into a file for easier reading), you can see the cloud attributes tree.

"cloud": {
"public_ipv4_addrs": [

"x.x.x.x"

],
"local_ipv4_addrs": [

"172.x.x.x"

],
"provider": "ec2",
"public_hostname": "ec2-x-x-x-x.compute-1.amazonaws.com",
"local_hostname": "ip-172-x-x-x.ec2.internal",
"public_ipv4": "x.x.x.x",
"local_ipv4": "172.x.x.x"
},

...

Depending on how you want to refactor your cookbooks, you could do a couple of things using the cloud attributes. You could create two sets of attributes with the cloud provider in the structure, or you could be clever and keep the attributes structure you have, but use a case statement:

default['application']['bucket'] = case node['cloud']['provider']
when 'azure'

"X"

when 'ec2'

"Y"

end

There's an example of this one in the openssh cookbook wrt platforms:

https://github.com/chef-cookbooks/openssh/blob/affe120e8cef47369be0f1d5d3960e423c748276/attributes/default.rb

I’ll tack on here, that you can get a specific bit of ohai from the commandline:
> ohai cloud for example (so you don’t have to weed through everything)

DJL

Mail](https://go.microsoft.com/fwlink/?LinkId=550986) for Windows 10

7A845521E8064BABB52BA37A8A74D4D0.png

Ohai is run as part of every chef-client run, it's included in your install of the chef client software. Chef Infra uses it to get everything from the hostname to IP addresses to the amount of memory your host has. so you don't have to think about it, you can just use what it finds out.

I found one rather complex example of an attributes file, in yum-epel.

https://github.com/chef-cookbooks/yum-epel/blob/ef264a3bb5435ddd6c925885bacff18d8ec8a2e8/attributes/epel.rb

honestly, all the attributes files in yum-epel are doing wacky things; there's just no good way to abstract what's going on there.

https://github.com/chef-cookbooks/yum-epel/tree/ef264a3bb5435ddd6c925885bacff18d8ec8a2e8/attributes

and there's a couple things in redisio that might suit too

https://github.com/sous-chefs/redisio/blob/5bee2714a94c0e6e2cd0be2422bc1aa80ebd7d88/attributes/default.rb

I figured it out via chef-shell. Documentation about ohai and cloud/provider needs a bit of updating.

default['carbonblack']['s3-bucket'] = case node['cloud']['provider']
when 'ec2'
default['carbonblack']['s3-bucket'] = "https://s3.amazonaws.com/core-#{node.chef_environment}/carbonblack"
else
default['carbonblack']['s3-bucket'] = "https://StorageAccount.blob.core.windows.net/core-#{node.chef_environment}/carbonblack"
end