Can an if statement apply to an organization?

I have a recipe that is applied to servers in two different Chef organizations. In the recipe there is a cookbook_file command that I would like to apply a different file depending on which organization the server is a member of. I naively tried “if node.chef_organization”, but that returns “Undefined method or attribute chef_organization' onnode’”. Is there a way to do this?


Chef Client is pretty ignorant about the existence of organizations in general. This is a historical artifact of organizations having been a paid feature in the past. Probably the easiest way to get the info is to parse the Chef::Config.chef_server_url with the ruby URI library, then get the path and split it with the Pathname library. For example:

require 'uri'; require 'pathname'
# => "testorg"

That code probably has a lot of edge cases that would not work, but should be fine for simple ones.

Thanks! I never would have thought of that. This works perfectly. For the record, here’s the code that works (chef_server_url is set in knife.rb):

require 'uri'; require 'pathname'
org = Pathname(URI("#{Config.chef_server_url}").path).split.last.to_s
if org == "foo"
  (do stuff)
  (do other stuff)