Skip to content

Dealing with multiple datacenters

Sean Walberg edited this page Feb 2, 2018 · 1 revision

If you have multiple datacenters it can be frustrating to have to pass the connection info on the command line, as you can only have one configuration in knife.rb. Or can you?

knife.rb is just Ruby code, so you can have conditionals in it and access the environment.

knife[:vsphere_host] = "default_vcenter.example.com"
knife[:vsphere_dc] = "DC1"
knife[:vsphere_user] = "myuser"
knife[:vsphere_pass] = "mypass"
if ENV['DC'] == 'DC2'
  knife[:vsphere_host] = "dc2vcenter.example.com"
  knife[:vsphere_dc] = "DC2"
end
# and so forth

If you prefix your command with DC=DC2 then the host and DC will be overridden. e.g.

DC=DC2 knife vsphere vm list # looks in DC2