API Reference

Chef API Interface

class chef.ChefAPI(url, key, client, version='0.10.8', headers={}, ssl_verify=True)

The ChefAPI object is a wrapper for a single Chef server.

The API stack

PyChef maintains a stack of ChefAPI objects to be use with other methods if an API object isn’t given explicitly. The first ChefAPI created will become the default, though you can set a specific default using ChefAPI.set_default(). You can also use a ChefAPI as a context manager to create a scoped default:

with ChefAPI('http://localhost:4000', 'client.pem', 'admin'):
    n = Node('web1')
classmethod from_config_file(path)

Load Chef API paraters from a config file. Returns None if the config can’t be used.

static get_global()

Return the API on the top of the stack.

set_default()

Make this the default API in the stack. Returns the old default if any.

chef.autoconfigure(base_path=None)

Try to find a knife or chef-client config file to load parameters from, starting from either the given base path or the current working directory.

The lookup order mirrors the one from Chef, first all folders from the base path are walked back looking for .chef/knife.rb, then ~/.chef/knife.rb, and finally /etc/chef/client.rb.

The first file that is found and can be loaded successfully will be loaded into a ChefAPI object.

Nodes

class chef.Node(name, api=None, skip_load=False)

A Chef node object.

The Node object can be used as a dict-like object directly, as an alias for the attributes data:

>>> node = Node('name')
>>> node['apache']['log_dir']
'/var/log/apache2'

New in version 0.1.

attributes

NodeAttributes corresponding to the composite of all precedence levels. This only uses the stored data on the Chef server, it does not merge in attributes from roles or environments on its own.

>>> node.attributes['apache']['log_dir']
'/var/log/apache2'
run_list

The run list of the node. This is the unexpanded list in type[name] format.

>>> node.run_list
['role[base]', 'role[app]', 'recipe[web]']
chef_environment

The name of the Chef Environment this node is a member of. This value will still be present, even if communicating with a Chef 0.9 server, but will be ignored.

New in version 0.2.

default

NodeAttributes corresponding to the default precedence level.

normal

NodeAttributes corresponding to the normal precedence level.

override

NodeAttributes corresponding to the override precedence level.

automatic

NodeAttributes corresponding to the automatic precedence level.

create(name, api=None, **kwargs)

Create a new object of this type. Pass the initial value for any attributes as keyword arguments.

delete(api=None)

Delete this object from the server.

list(api=None)

Return a ChefQuery with the available objects of this type.

save(api=None)

Save this object to the server. If the object does not exist it will be created.

class chef.node.NodeAttributes(search_path=[], path=None, write=None)

A collection of Chef Node attributes.

Attributes can be accessed like a normal python dict:

print node['fqdn']
node['apache']['log_dir'] = '/srv/log'

When writing to new attributes, any dicts required in the hierarchy are created automatically.

New in version 0.1.

get_dotted(key)

Retrieve an attribute using a dotted key path. A dotted path is a string of the form ‘foo.bar.baz’, with each . separating hierarcy levels.

Example:

node.attributes['apache']['log_dir'] = '/srv/log'
print node.attributes.get_dotted('apache.log_dir')
has_dotted(key)

Check if a given dotted key path is present. See get_dotted() for more information on dotted paths.

New in version 0.2.

set_dotted(key, value)

Set an attribute using a dotted key path. See get_dotted() for more information on dotted paths.

Example:

node.attributes.set_dotted('apache.log_dir', '/srv/log')

Roles

class chef.Role(name, api=None, skip_load=False)

A Chef role object.

create(name, api=None, **kwargs)

Create a new object of this type. Pass the initial value for any attributes as keyword arguments.

delete(api=None)

Delete this object from the server.

list(api=None)

Return a ChefQuery with the available objects of this type.

save(api=None)

Save this object to the server. If the object does not exist it will be created.

Data Bags

class chef.DataBag(name, api=None, skip_load=False)

A Chef data bag object.

Data bag items are available via the mapping API. Evaluation works in the same way as ChefQuery, so requesting only the names will not cause the items to be loaded:

bag = DataBag('versions')
item = bag['web']
for name, item in six.iteritems(bag):
    print item['qa_version']
create(name, api=None, **kwargs)

Create a new object of this type. Pass the initial value for any attributes as keyword arguments.

delete(api=None)

Delete this object from the server.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
list(api=None)

Return a ChefQuery with the available objects of this type.

save(api=None)

Save this object to the server. If the object does not exist it will be created.

values() → list of D's values
class chef.DataBagItem(bag, name, api=None, skip_load=False)

A Chef data bag item object.

Data bag items act as normal dicts and can contain arbitrary data.

bag

The DataBag this item is a member of.

clear() → None. Remove all items from D.
classmethod create(bag, name, api=None, **kwargs)

Create a new data bag item. Pass the initial value for any keys as keyword arguments.

delete(api=None)

Delete this object from the server.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
list(api=None)

Return a ChefQuery with the available objects of this type.

pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

save(api=None)

Save this object to the server. If the object does not exist it will be created.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() → list of D's values

Environments

class chef.Environment(name, api=None, skip_load=False)

A Chef environment object.

New in version 0.2.

create(name, api=None, **kwargs)

Create a new object of this type. Pass the initial value for any attributes as keyword arguments.

delete(api=None)

Delete this object from the server.

list(api=None)

Return a ChefQuery with the available objects of this type.

save(api=None)

Save this object to the server. If the object does not exist it will be created.