Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Architecture and Setup

Ohai and the Node Object

Learn how Ohai collects facts about a machine, what the node object holds, which automatic attributes you use most, and how recipes branch on platform and memory.

Before a recipe can decide anything, Chef needs to know what machine it is on. Ohai is the tool that finds out. It gathers dozens of facts (operating system, CPU, memory, network, installed kernel, cloud provider) and hands them to Chef as the automatic attributes of the node object. In this lesson you learn which attributes matter most, how recipes read them, and how to practice with a stand-in because Ohai cannot inspect a real server here.

What Ohai does

Ohai is a stand-alone program that prints JSON. Running it on a node is read-only here, but the command is simply:

Terminal
ohai
ohai platform_family
ohai memory/total

The output is a large nested JSON document. At the start of every Chef run, Ohai executes and the result becomes part of the node object. The values are called automatic attributes because Chef sets them for you and they have the highest precedence, so recipes cannot override them accidentally.

The attributes you will use most

A small selection, with realistic values:

Attribute Example Meaning
node['platform'] 'ubuntu' The exact OS
node['platform_family'] 'debian' Group of related OSes
node['platform_version'] '24.04' Version
node['hostname'] 'web1' Short host name
node['fqdn'] 'web1.example.com' Full name
node['ipaddress'] '10.0.0.11' Main IP
node['cpu']['total'] 4 Number of CPUs
node['memory']['total'] '8167436kB' Memory as text
node['kernel']['machine'] 'x86_64' Architecture

platform_family is the one to branch on: debian covers Ubuntu and Debian, rhel covers Red Hat, Rocky, Alma, CentOS and Fedora-like systems. Writing if platform_family?('debian') is far shorter than listing every distribution.

Ohai values are strings. Memory comes as text with a unit, such as '8167436kB', so parse it before doing arithmetic.

The node object

The node object is a Ruby hash-like object that holds everything about the machine: automatic attributes from Ohai, attributes from cookbooks, roles and environments, the run list and the name. In recipes it appears as the variable node. These real calls are read-only:

Ruby
node['platform_family']              # read an attribute
node['nginx']['port']                # nested attribute
node.name                            # node name
node.run_list                        # expanded run list
node.chef_environment                # environment name
platform_family?('debian')           # helper method
platform?('ubuntu', 'debian')

At the end of each run the whole object is saved to the Chef Infra Server (this is what you see with knife node show).

Stand-in: a fake Ohai and a recipe that adapts

The stand-in builds a fake node hash and a recipe that picks the right package and web root. It is plain Ruby and not real Ohai.

Ruby Stand-in: branching on platform_family

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The same recipe produces correct results on two families. That is the main reason Ohai exists: write once, adapt to the machine.

Parsing memory safely

Because memory is a string with kB, a typical recipe converts it. Try the conversion:

Ruby Stand-in: converting Ohai memory to MB

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

'8167436kB'.to_i reads the leading digits and ignores the rest, which makes it a convenient way to strip the unit.

Automatic attributes are read-only in practice

You may try to assign node.default['platform'] = 'foo', but the automatic level wins on read, so your value is ignored. Treat automatic attributes as facts and never fight them. If you need a different value, create your own attribute (for example node['myapp']['os_label']) and set it from the fact.

Custom Ohai plugins

When you need a fact Ohai does not collect (the version of an internal tool, a rack position) you can write an Ohai plugin, a small Ruby file that adds data under a new key. The plugin is distributed with a cookbook and loaded with the ohai resource. A minimal real plugin, read-only:

Ruby
Ohai.plugin(:AppVersion) do
  provides 'app_version'
  collect_data do
    app_version shell_out('/opt/app/bin/app --version').stdout.strip
  end
end

After the plugin runs, node['app_version'] is available to every recipe and to search.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Ohai produce?

    Ohai collects system information and Chef stores it in the node object as automatic attributes.
  2. 2. Which attribute is best for a recipe that must work on both Ubuntu and Debian?

    platform_family groups related operating systems, such as debian for Ubuntu and Debian.
  3. 3. node['memory']['total'] returns '8167436kB'. How do you get a number?

    Ohai values are strings. .to_i extracts the leading integer and drops the unit.
  4. 4. Why are automatic attributes hard to override?

    Automatic is the top precedence level, so facts always win over defaults and overrides.

Summary

  • Ohai gathers facts at the start of each run and stores them as automatic attributes.
  • The node object combines facts, cookbook attributes, roles, environments and the run list.
  • Branch on platform_family, not on each distribution.
  • Custom Ohai plugins add your own facts.

Next: Chef Infra Server and Bootstrapping