OSChefInfra.006: Chef Custom Resources — Typed Properties, Actions and Unified Mode

Original technical illustration showing Chef Infra unified mode, a typed custom resource property, create action, and two target nodes with declared file state. BitcoinVersus.tech watermark.

Open-Source Chef Infrastructure • OSChefInfra.006 • 35–45 minutes

A cookbook becomes difficult to maintain when every recipe repeats the same collection of low-level resources: a custom resource turns that repeated configuration into a reusable interface with declared properties and actions. Unified mode makes the execution of its action block easier to reason about by processing its resources and Ruby logic in a single pass.

Learning objectives and prerequisites

Declare typed properties, call a custom resource, explain unified mode, and separate syntax validation from actual convergence. Review resource convergence, cookbook structure, and Policyfiles.

Expose intent instead of copying implementation

The Chef Infra custom resource guide describes a resource as a reusable collection of actions and properties. A property specifies an input’s type and can require it or derive it from the declared resource name. The action uses native Chef resources for changes that need convergence behavior. In Chef Infra Client 18 and later, unified mode defaults to enabled; an explicit declaration also makes the intended behavior clear. See unified mode documentation for notification and ordering considerations.

Progress Chef, ChefConf 2016: a conference talk on composable cookbooks and custom resources. Historical design principles; use current Chef documentation for syntax and unified mode defaults. Original source.
Rack servers viewed from the rear with network cabling
Abigor, February 2, 2011. Original photograph, CC BY-SA 3.0. Illustrative managed hardware; no Chef deployment is shown.

Laboratory: define a message resource

Create a disposable cookbook named training_lab and place the first file at resources/message.rb. The name property maps the resource instance name to path. The required message is a separate typed input. The built-in file resource manages the contents and permissions.

unified_mode true
property :path, String, name_property: true
property :message, String, required: true
default_action :create
action :create do
  file new_resource.path do
    content "#{new_resource.message}\n"
    mode '0644'
    action :create
  end
end

Place the following invocation in recipes/default.rb. Use a disposable authorized Linux instance and ensure the example target path is reserved for the lab; do not overwrite unrelated files.

training_lab_message '/tmp/chef-resource-lab.txt' do
  message 'hello from Chef'
end

Ruby 3.3.8 syntax checks returned Syntax OK for both files. Chef Infra Client was not installed in the test environment, so resource registration, the required-property check and actual convergence were not executed. The illustration is schematic; the two complete code blocks above are the lab source.

Verify the behavior in a real Chef test node

Run the recipe twice using a supported Chef installation and the locally prepared cookbook under an authorized test policy. The first converge should produce the declared text and mode. With no drift, the second should avoid updating the file. Change the declared message and verify the expected update. Check the file and mode independently with the methods in Test Kitchen and InSpec. These are expected outcomes, not observed Chef execution results.

June 2020 practitioner discussion of separate cookbook/custom resource repositories and Policyfile versioning. Compare deployment organization with the interface defined here; the comments are historical experience, not current Chef documentation. Original source.

Maintainability and safety

Keep a resource interface narrow; validate inputs and avoid adding secrets to log output. Using unified mode does not guarantee idempotency when arbitrary Ruby code causes side effects. Use resource actions and tests rather than unconditionally rewriting files. The historical Chef 18 release discussion documents the default change. Do not deploy a new resource to production without a reviewed policy, test results and rollback plan.

Knowledge check

1. What does name_property do? 2. Why call the built-in file resource? 3. Does ruby -c demonstrate idempotency? 4. What does unified mode simplify?

Answers: 1. It maps the resource name to the declared property. 2. It expresses desired file state using Chef’s convergence logic. 3. No; it validates Ruby syntax. 4. Ordering within a custom resource action, not every dependency or notification in a cookbook.

Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True.

BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply