Writing Chef Target Mode Resources

Thumbnail

Writing Chef Target Mode Resources

After my previous blog posts, you might be tempted to write your own Chef custom resources which are compatible with Target Mode.

Luckily, this is very easy - so this will be a short one.

When writing resources, you can use a lightweight DSL which looks like this:

resource_name :esx_config

description "This resource configures vSphere ESX hosts"

... and so on ...

The trick to have this work with target mode lies in adding provides lines which specify target mode as well as the supported platforms. This is easily done by adding a Boolean flag as well as the relevant platforms or platform_families

resource_name :esx_config

description "This resource configures vSphere ESX hosts"

provides :esx_config, target_mode: true, platform: 'vmkernel'
provides :esx_config, target_mode: true, platform_family: 'esx'

... and so on ...

Within your custom resource, you have access to all the methods and properties that chef provides, as even remote node_attributes are transparently managed with connected Chef servers. If you just wrap Chef-native resources to have more convenience over repeated tasks (sometimes also called Composite Custom Resources), please check my last blog post for escaping Target Mode for local preprocessing. If you work with a plain Ruby Custom Resource, this is not neccessary though.

Hopefully this post saves you a bit of digging in the Chef source code, see you next time.