Ohai is the system profiler that Chef Infra Client runs at the start of every run. It collects facts such as platform, IP address, memory and cloud metadata and exposes them to recipes as automatic attributes under node[...]. This page explains the example recipe above and fixes one bug in it. It also shows how to query Ohai from the command line, use its data safely in cookbooks and write a custom Ohai plugin.
Ohai is a tool for collecting system configuration data, which it then provides to Chef Infra Client to use in cookbooks. Chef Infra Client runs Ohai at the start of every Chef Infra run to determine system state. The attributes that Ohai collects are called automatic attributes. Chef Infra Client uses these attributes to ensure that nodes are in the desired state after each configuration run.
The types of attributes Ohai collects include but are not limited to:
- Operating System
- Network
- Memory
- Disk
- CPU
- Kernel
- Host names
- Fully qualified domain names
- Virtualization
- Cloud provider metadata
Ohai collects data for many platforms, including
- AIX,
- macOS,
- Linux,
- FreeBSD,
- Solaris, and
- any Windows operating systems.

How Ohai fits into a Chef Infra run
When chef-client starts, it runs Ohai before it compiles any recipes. Ohai's plugins inspect the operating system and build a large tree of data. Chef Infra Client merges that tree into the node object as automatic attributes. In a client-server setup, the node object is then saved to the Chef Infra Server at the end of the run, so other nodes and knife search can query it.
- Automatic attributes have the highest precedence. Cookbooks, roles and environments cannot override them.
- They are rebuilt from scratch on every run, so they always describe the current state of the machine.
- They are read-only in recipes. If you need your own value, use a default or override attribute with a different name.
Exploring Ohai data from the command line
Look at the raw data before you write a recipe. The ohai binary is installed with both Chef Workstation and Chef Infra Client. With no arguments it prints the full attribute tree as JSON. If you pass an attribute path, it prints only that branch.
# Full JSON output (long, so page it)
ohai | less
# Single top-level attributes
ohai platform
ohai platform_version
ohai ipaddress
# Nested attributes use a slash-separated path
ohai kernel/release
ohai memory/total
ohai network/default_interface
# If ohai is not on PATH
/opt/chef/bin/ohai platform # node with Chef Infra Client
/opt/chef-workstation/bin/ohai platform # Chef WorkstationCode language: PHP (php)
Each query returns a JSON value, for example ["ubuntu"] or ["10.0.2.15"]. A slash-separated path on the command line maps to a nested lookup in a recipe, so kernel/release becomes node['kernel']['release'].
Note: Run ohai as the same user that runs chef-client, which is usually root. Some plugins return less data when they run as an unprivileged user.
Corrected example recipe
The recipe above has a bug in its last log resource. It is meant to print when Ohai last ran, but it reads node['recipes'] a second time. The correct attribute is node['ohai_time'], which holds a Unix epoch timestamp. In this version, the timestamp is converted to a readable time and the recipe list is joined into a string, so the log does not show Ruby array syntax.
# cookbooks/ohai_demo/recipes/default.rb
log "Platform: #{node['platform']}"
log "Platform family (Chef-specific grouping, e.g. debian, rhel): #{node['platform_family']}"
log "Platform version: #{node['platform_version']}"
log "IP address: #{node['ipaddress']}"
log "MAC address: #{node['macaddress']}"
log "FQDN: #{node['fqdn']}"
log "Host name: #{node['hostname']}"
log "Domain: #{node['domain']}"
log "Recipes in run list: #{node['recipes'].join(', ')}"
log "Ohai last ran at: #{Time.at(node['ohai_time'])}"
# Nested attributes
log "Kernel release: #{node['kernel']['release']}"
log "Total memory: #{node['memory']['total']}"
log "CPU count: #{node['cpu']['total']}"
# Attributes that may not exist on every node: use dig to avoid nil errors
cloud_provider = node.dig('cloud', 'provider') || 'none detected'
log "Cloud provider: #{cloud_provider}"Code language: PHP (php)
level :info is the default for the log resource, so the corrected version leaves out the do ... end blocks. Add level :warn to a resource only if you want that message to appear without raising the client log level.
Running the recipe locally
- Generate a cookbook on a machine with Chef Workstation:
chef generate cookbook cookbooks/ohai_demo. - Replace
cookbooks/ohai_demo/recipes/default.rbwith the corrected recipe. - Run it in local mode from the directory that contains
cookbooks/:sudo chef-client -z -o 'recipe[ohai_demo]' -l info --chef-license accept. - Check the output for lines such as
INFO: Platform: ubuntuandINFO: Ohai last ran at: ....
Note: -l info matters here. With the default log level and formatter, log resources at :info level often produce no visible output, and readers then assume the recipe failed.
Using Ohai data to make decisions
Printing attributes is useful for learning. In real cookbooks, Ohai data is mostly used to pick the right package, path or template for each platform. Chef provides helper methods such as platform? and platform_family?, so you do not need to compare strings by hand.
web_pkg = platform_family?('rhel', 'fedora', 'amazon') ? 'httpd' : 'apache2'
package web_pkg
service web_pkg do
action [:enable, :start]
end
template '/etc/motd' do
source 'motd.erb'
variables(
hostname: node['hostname'],
ip: node['ipaddress'],
memory: node['memory']['total']
)
endCode language: JavaScript (javascript)
On a Chef Infra Server, the same data can be searched across your fleet with knife. For example, knife search node 'platform_family:debian' -a ipaddress lists the IP address of every Debian-family node, and knife node show web01 -a platform_version shows one attribute for one node. For more knife options, see the knife command line reference.
Writing a custom Ohai plugin
If a fact you need is not collected by default, you can write a plugin. Put it in the ohai/ directory of a cookbook. Chef Infra Client copies plugins from that directory to the node and loads them automatically.
# cookbooks/ohai_demo/ohai/app_info.rb
Ohai.plugin(:AppInfo) do
provides 'app_info'
collect_data(:linux) do
app_info Mash.new
app_info[:release_file] = ::File.exist?('/opt/myapp/RELEASE')
if app_info[:release_file]
app_info[:version] = ::File.read('/opt/myapp/RELEASE').strip
end
so = shell_out('uptime -p')
app_info[:uptime] = so.stdout.strip if so.exitstatus == 0
end
endCode language: PHP (php)
providesdeclares the top-level attribute name, and it must match the method you call (app_info).collect_data(:linux)runs only on Linux. Use:defaultfor code that runs on every platform.shell_outis available inside plugins, so you do not need backticks orsystem.- Read the data in recipes as
node['app_info']['version']. - To test a plugin outside Chef, run
ohai -d /path/to/plugin_dir app_info.
Common problems and fixes
| Symptom | Cause | Fix |
|---|---|---|
log messages do not appear | Client log level is above info | Run with -l info or set level :warn on the resource |
NoMethodError: undefined method '[]' for nil | Nested attribute missing on this node (e.g. cloud on bare metal) | Use node.dig('cloud', 'provider') and handle nil |
Recipe list prints as ["a::default"] | An array was interpolated directly | Use node['recipes'].join(', ') |
ohai: command not found | Binary not on PATH | Call /opt/chef/bin/ohai or /opt/chef-workstation/bin/ohai |
| Custom plugin data missing | Plugin not in the cookbook's ohai/ directory, or provides name mismatch | Check the path and test with ohai -d <dir> <attribute> |
| Runs are slow or node objects are large | Expensive plugins such as Passwd are enabled | Add ohai.disabled_plugins = [:Passwd] to client.rb |
Frequently asked questions
What are automatic attributes in Chef?
Automatic attributes are the system facts that Ohai collects at the start of each Chef Infra Client run. They have the highest attribute precedence, are rebuilt on every run, and cannot be overridden by cookbooks, roles or environments.
How do I see all Ohai attributes for a node?
Run ohai on the node to print the full JSON tree, or pass a path such as ohai network/default_interface to print one branch. If the node is registered with a Chef Infra Server, knife node show NODE -l shows the saved attributes.
What is the difference between platform and platform_family?
platform is the specific distribution, such as ubuntu, centos or windows. platform_family is a Chef grouping of related platforms, such as debian for Ubuntu and Debian or rhel for RHEL, CentOS, Rocky and Alma Linux. Use the family when platforms share the same package names and paths.
Can I override an Ohai attribute such as ipaddress?
Not from a cookbook, because automatic attributes always win. If Ohai picks the wrong interface, store the value you need in your own attribute, or write a custom Ohai plugin that collects the correct value.
Where do custom Ohai plugins go?
Put them in the ohai/ directory of a cookbook. Chef Infra Client then delivers and loads them automatically during the run. For standalone testing, point the CLI at the directory with ohai -d <dir>.
Related tutorials
- Chef Tutorials: knife command line reference
- What is Chef and use cases of Chef?
- More Chef tutorials
- Top 50 Chef interview questions and answers
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals