{"id":31852,"date":"2026-12-01T22:04:19","date_gmt":"2026-12-01T22:04:19","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31852"},"modified":"2026-12-01T22:04:19","modified_gmt":"2026-12-01T22:04:19","slug":"chef-tutorials-working-with-ohai","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-working-with-ohai\/","title":{"rendered":"Chef Tutorials: Working with Ohai"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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 <code>node[...]<\/code>. 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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The types of attributes Ohai collects include but are not limited to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Operating System<\/li>\n\n\n\n<li>Network<\/li>\n\n\n\n<li>Memory<\/li>\n\n\n\n<li>Disk<\/li>\n\n\n\n<li>CPU<\/li>\n\n\n\n<li>Kernel<\/li>\n\n\n\n<li>Host names<\/li>\n\n\n\n<li>Fully qualified domain names<\/li>\n\n\n\n<li>Virtualization<\/li>\n\n\n\n<li>Cloud provider metadata<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Ohai collects data for many platforms, including<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AIX,<\/li>\n\n\n\n<li>macOS,<\/li>\n\n\n\n<li>Linux,<\/li>\n\n\n\n<li>FreeBSD,<\/li>\n\n\n\n<li>Solaris, and<\/li>\n\n\n\n<li>any Windows operating systems.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"901\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-22.png\" alt=\"Terminal running ohai | more, showing JSON kernel data such as Linux release and modules\" class=\"wp-image-31853\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-22.png 837w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-22-279x300.png 279w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-22-768x827.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/768bfbc42c274f27a41af8dfef4a2c0c.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How Ohai fits into a Chef Infra run<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>chef-client<\/code> starts, it runs Ohai before it compiles any recipes. Ohai&#039;s plugins inspect the operating system and build a large tree of data. Chef Infra Client merges that tree into the node object as <strong>automatic attributes<\/strong>. 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 <code>knife search<\/code> can query it.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatic attributes have the <strong>highest precedence<\/strong>. Cookbooks, roles and environments cannot override them.<\/li>\n\n\n<li>They are rebuilt from scratch on every run, so they always describe the current state of the machine.<\/li>\n\n\n<li>They are read-only in recipes. If you need your own value, use a default or override attribute with a different name.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Exploring Ohai data from the command line<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the raw data before you write a recipe. The <code>ohai<\/code> 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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Full JSON output (long, so page it)<\/span>\nohai | less\n\n<span class=\"hljs-comment\"># Single top-level attributes<\/span>\nohai platform\nohai platform_version\nohai ipaddress\n\n<span class=\"hljs-comment\"># Nested attributes use a slash-separated path<\/span>\nohai kernel\/release\nohai memory\/total\nohai network\/default_interface\n\n<span class=\"hljs-comment\"># If ohai is not on PATH<\/span>\n\/opt\/chef\/bin\/ohai platform              <span class=\"hljs-comment\"># node with Chef Infra Client<\/span>\n\/opt\/chef-workstation\/bin\/ohai platform  <span class=\"hljs-comment\"># Chef Workstation<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Each query returns a JSON value, for example <code>[&quot;ubuntu&quot;]<\/code> or <code>[&quot;10.0.2.15&quot;]<\/code>. A slash-separated path on the command line maps to a nested lookup in a recipe, so <code>kernel\/release<\/code> becomes <code>node[&#039;kernel&#039;][&#039;release&#039;]<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Run <code>ohai<\/code> as the same user that runs <code>chef-client<\/code>, which is usually root. Some plugins return less data when they run as an unprivileged user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected example recipe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The recipe above has a bug in its last <code>log<\/code> resource. It is meant to print when Ohai last ran, but it reads <code>node[&#039;recipes&#039;]<\/code> a second time. The correct attribute is <code>node[&#039;ohai_time&#039;]<\/code>, 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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># cookbooks\/ohai_demo\/recipes\/default.rb<\/span>\nlog <span class=\"hljs-string\">\"Platform: #{node&#91;'platform']}\"<\/span>\nlog <span class=\"hljs-string\">\"Platform family (Chef-specific grouping, e.g. debian, rhel): #{node&#91;'platform_family']}\"<\/span>\nlog <span class=\"hljs-string\">\"Platform version: #{node&#91;'platform_version']}\"<\/span>\nlog <span class=\"hljs-string\">\"IP address: #{node&#91;'ipaddress']}\"<\/span>\nlog <span class=\"hljs-string\">\"MAC address: #{node&#91;'macaddress']}\"<\/span>\nlog <span class=\"hljs-string\">\"FQDN: #{node&#91;'fqdn']}\"<\/span>\nlog <span class=\"hljs-string\">\"Host name: #{node&#91;'hostname']}\"<\/span>\nlog <span class=\"hljs-string\">\"Domain: #{node&#91;'domain']}\"<\/span>\nlog <span class=\"hljs-string\">\"Recipes in run list: #{node&#91;'recipes'].join(', ')}\"<\/span>\nlog <span class=\"hljs-string\">\"Ohai last ran at: #{Time.at(node&#91;'ohai_time'])}\"<\/span>\n\n<span class=\"hljs-comment\"># Nested attributes<\/span>\nlog <span class=\"hljs-string\">\"Kernel release: #{node&#91;'kernel']&#91;'release']}\"<\/span>\nlog <span class=\"hljs-string\">\"Total memory: #{node&#91;'memory']&#91;'total']}\"<\/span>\nlog <span class=\"hljs-string\">\"CPU count: #{node&#91;'cpu']&#91;'total']}\"<\/span>\n\n<span class=\"hljs-comment\"># Attributes that may not exist on every node: use dig to avoid nil errors<\/span>\ncloud_provider = node.dig(<span class=\"hljs-string\">'cloud'<\/span>, <span class=\"hljs-string\">'provider'<\/span>) || <span class=\"hljs-string\">'none detected'<\/span>\nlog <span class=\"hljs-string\">\"Cloud provider: #{cloud_provider}\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>level :info<\/code> is the default for the <code>log<\/code> resource, so the corrected version leaves out the <code>do ... end<\/code> blocks. Add <code>level :warn<\/code> to a resource only if you want that message to appear without raising the client log level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running the recipe locally<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Generate a cookbook on a machine with Chef Workstation: <code>chef generate cookbook cookbooks\/ohai_demo<\/code>.<\/li>\n\n\n<li>Replace <code>cookbooks\/ohai_demo\/recipes\/default.rb<\/code> with the corrected recipe.<\/li>\n\n\n<li>Run it in local mode from the directory that contains <code>cookbooks\/<\/code>: <code>sudo chef-client -z -o &#039;recipe[ohai_demo]&#039; -l info --chef-license accept<\/code>.<\/li>\n\n\n<li>Check the output for lines such as <code>INFO: Platform: ubuntu<\/code> and <code>INFO: Ohai last ran at: ...<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> <code>-l info<\/code> matters here. With the default log level and formatter, <code>log<\/code> resources at <code>:info<\/code> level often produce no visible output, and readers then assume the recipe failed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Ohai data to make decisions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>platform?<\/code> and <code>platform_family?<\/code>, so you do not need to compare strings by hand.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">web_pkg = platform_family?(<span class=\"hljs-string\">'rhel'<\/span>, <span class=\"hljs-string\">'fedora'<\/span>, <span class=\"hljs-string\">'amazon'<\/span>) ? <span class=\"hljs-string\">'httpd'<\/span> : <span class=\"hljs-string\">'apache2'<\/span>\n\npackage web_pkg\n\nservice web_pkg <span class=\"hljs-keyword\">do<\/span>\n  action &#91;:enable, :start]\nend\n\ntemplate <span class=\"hljs-string\">'\/etc\/motd'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source <span class=\"hljs-string\">'motd.erb'<\/span>\n  variables(\n    hostname: node&#91;<span class=\"hljs-string\">'hostname'<\/span>],\n    <span class=\"hljs-attr\">ip<\/span>:       node&#91;<span class=\"hljs-string\">'ipaddress'<\/span>],\n    <span class=\"hljs-attr\">memory<\/span>:   node&#91;<span class=\"hljs-string\">'memory'<\/span>]&#91;<span class=\"hljs-string\">'total'<\/span>]\n  )\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">On a Chef Infra Server, the same data can be searched across your fleet with knife. For example, <code>knife search node &#039;platform_family:debian&#039; -a ipaddress<\/code> lists the IP address of every Debian-family node, and <code>knife node show web01 -a platform_version<\/code> shows one attribute for one node. For more knife options, see the <a href=\"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-knife-command-line-reference\/\">knife command line reference<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing a custom Ohai plugin<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If a fact you need is not collected by default, you can write a plugin. Put it in the <code>ohai\/<\/code> directory of a cookbook. Chef Infra Client copies plugins from that directory to the node and loads them automatically.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># cookbooks\/ohai_demo\/ohai\/app_info.rb<\/span>\nOhai.plugin(:AppInfo) <span class=\"hljs-keyword\">do<\/span>\n  provides <span class=\"hljs-string\">'app_info'<\/span>\n\n  collect_data(:linux) <span class=\"hljs-keyword\">do<\/span>\n    app_info Mash.<span class=\"hljs-keyword\">new<\/span>\n    app_info&#91;:release_file] = ::File.exist?(<span class=\"hljs-string\">'\/opt\/myapp\/RELEASE'<\/span>)\n    <span class=\"hljs-keyword\">if<\/span> app_info&#91;:release_file]\n      app_info&#91;:version] = ::File.read(<span class=\"hljs-string\">'\/opt\/myapp\/RELEASE'<\/span>).strip\n    end\n    so = shell_out(<span class=\"hljs-string\">'uptime -p'<\/span>)\n    app_info&#91;:uptime] = so.stdout.strip <span class=\"hljs-keyword\">if<\/span> so.exitstatus == <span class=\"hljs-number\">0<\/span>\n  end\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>provides<\/code> declares the top-level attribute name, and it must match the method you call (<code>app_info<\/code>).<\/li>\n\n\n<li><code>collect_data(:linux)<\/code> runs only on Linux. Use <code>:default<\/code> for code that runs on every platform.<\/li>\n\n\n<li><code>shell_out<\/code> is available inside plugins, so you do not need backticks or <code>system<\/code>.<\/li>\n\n\n<li>Read the data in recipes as <code>node[&#039;app_info&#039;][&#039;version&#039;]<\/code>.<\/li>\n\n\n<li>To test a plugin outside Chef, run <code>ohai -d \/path\/to\/plugin_dir app_info<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common problems and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>log<\/code> messages do not appear<\/td><td>Client log level is above <code>info<\/code><\/td><td>Run with <code>-l info<\/code> or set <code>level :warn<\/code> on the resource<\/td><\/tr><tr><td><code>NoMethodError: undefined method &#039;[]&#039; for nil<\/code><\/td><td>Nested attribute missing on this node (e.g. <code>cloud<\/code> on bare metal)<\/td><td>Use <code>node.dig(&#039;cloud&#039;, &#039;provider&#039;)<\/code> and handle <code>nil<\/code><\/td><\/tr><tr><td>Recipe list prints as <code>[&quot;a::default&quot;]<\/code><\/td><td>An array was interpolated directly<\/td><td>Use <code>node[&#039;recipes&#039;].join(&#039;, &#039;)<\/code><\/td><\/tr><tr><td><code>ohai: command not found<\/code><\/td><td>Binary not on <code>PATH<\/code><\/td><td>Call <code>\/opt\/chef\/bin\/ohai<\/code> or <code>\/opt\/chef-workstation\/bin\/ohai<\/code><\/td><\/tr><tr><td>Custom plugin data missing<\/td><td>Plugin not in the cookbook&#039;s <code>ohai\/<\/code> directory, or <code>provides<\/code> name mismatch<\/td><td>Check the path and test with <code>ohai -d &lt;dir&gt; &lt;attribute&gt;<\/code><\/td><\/tr><tr><td>Runs are slow or node objects are large<\/td><td>Expensive plugins such as <code>Passwd<\/code> are enabled<\/td><td>Add <code>ohai.disabled_plugins = [:Passwd]<\/code> to <code>client.rb<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What are automatic attributes in Chef?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I see all Ohai attributes for a node?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run <code>ohai<\/code> on the node to print the full JSON tree, or pass a path such as <code>ohai network\/default_interface<\/code> to print one branch. If the node is registered with a Chef Infra Server, <code>knife node show NODE -l<\/code> shows the saved attributes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between platform and platform_family?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>platform<\/code> is the specific distribution, such as <code>ubuntu<\/code>, <code>centos<\/code> or <code>windows<\/code>. <code>platform_family<\/code> is a Chef grouping of related platforms, such as <code>debian<\/code> for Ubuntu and Debian or <code>rhel<\/code> for RHEL, CentOS, Rocky and Alma Linux. Use the family when platforms share the same package names and paths.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I override an Ohai attribute such as ipaddress?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where do custom Ohai plugins go?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Put them in the <code>ohai\/<\/code> 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 <code>ohai -d &lt;dir&gt;<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-knife-command-line-reference\/\">Chef Tutorials: knife command line reference<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-chef-and-use-cases-of-chef\/\">What is Chef and use cases of Chef?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/chef\/\">More Chef tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-chef-interview-questions-and-answers\/\">Top 50 Chef interview questions and answers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[88],"tags":[1659],"class_list":["post-31852","post","type-post","status-publish","format-standard","hentry","category-chef","tag-chef-cookbooks"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31852"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31852\/revisions"}],"predecessor-version":[{"id":80487,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31852\/revisions\/80487"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}