Writing Chef Target Mode Resources

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.

Similar Posts You Might Enjoy

Target Mode with Serial Devices

Target Mode with Serial Devices Usually, you will work with SSH or WinRM to connect to remote nodes and configure them. Those standard protocols bring along all the perks of a modern network connection: Encryption, Authentication, File transfers, etc But what if you have a device without network connectivity? - by Thomas Heinen

Custom Resource Diffs in Chef

Custom Resource Diffs in Chef If you are writing custom resources regularly, you might have been annoyed by a general “diff” functionality in Chef. In this post we will work on some snippets to make this possible - by Thomas Heinen

Local Preprocessing in Target Mode

Local Preprocessing in Target Mode If you ever created configuration files with any automation system, you know that this involves a lot of templating. This is actually one of the most basic tasks that Chef performs and it is done using the template resource. With Chef’s Target Mode this currently is a bit more complicated. - by Thomas Heinen