Get Chef Server Address Via Python

I have multiple chef servers and a secondary servers with chefdk. I am trying to find a way to return the chef server IP into a python script. I’ve looked at the documentation for chef, chef-client and knife, there doesn’t appear to be a way to just return the chef server IP or hostname. Any thoughts on where I can get this value to use in my scripts?


You can get the full URL from chef or knife as Chef::Config[:chef_server_url]:

knife exec -E 'puts Chef::Config[:chef_server_url]'

PyChef’s config autoloader does this for you. You can use that and then look at the returned ChefAPI object.

Thanks! This worked:

chefserverpull = "/usr/bin/knife exec -E 'puts Chef::Config[:chef_server_url]'"
urlchefserver = commands.getoutput(chefserverpull)
chefserver = urlparse(urlchefserver)
print "Chef Server: " + chefserver.hostname + "\n"

Maybe not the most elegant, but it does the job.

I’ll have to take a look at PyChef when I have more time…