Chef Ohai Tutorials
What is Ohai?
Ohai is a tool that is used to collect system configuration data, which is provided to the chef-client for use within cookbooks. Ohai is run by the chef-client at the beginning of every Chef run to determine system state. Ohai includes many built-in plugins to detect common configuration details as well as a plugin model for writing custom plugins.
ohai is the command-line interface for Ohai, a tool that is used to detect attributes on a node, and then provide these attributes to the chef-client at the start of every chef-client run.
The types of attributes Ohai collects include but are not limited to:
- Operating System
- Host names
- Fully qualified domain names
- Cloud provider metadata
Attributes that are collected by Ohai are automatic level attributes, in that these attributes are used by the chef-client to ensure that these attributes remain unchanged after the chef-client is done configuring the node.
How Ohai can be installed?Its part of Chef Client installation.
How to list out all the Automatic Attributes?
$ cd /opt/chefdk/embedded/lib/ruby/gems/*/gems/ohai-*/lib $ ohai
Some of the popular Automatic Attributes
An automatic attribute is a specific detail about a node, such as an IP address, a host name, a list of loaded kernel modules, and so on.
|node['platform']||The platform on which a node is running. This attribute helps determine which providers will be used.|
|node['platform_version']||The version of the platform. This attribute helps determine which providers will be used.|
|node['ipaddress']||The IP address for a node. If the node has a default route, this is the IPV4 address for the interface. If the node does not have a default route, the value for this attribute should be nil. The IP address for default route is the recommended default value.|
|node['macaddress']||The MAC address for a node, determined by the same interface that detects the node['ipaddress'].|
|node['fqdn']||The fully qualified domain name for a node. This is used as the name of a node unless otherwise set.|
|node['hostname']||The host name for the node.|
|node['domain']||The domain for the node.|
|node['recipes']||A list of recipes associated with a node (and part of that node’s run-list).|
|node['roles']||A list of roles associated with a node (and part of that node’s run-list).|
|node['ohai_time']||The time at which Ohai was last run. This attribute is not commonly used in recipes, but it is saved to the Chef server and can be accessed using the knife status subcommand.|
Example of Chef Ohai Resources
#Reload Ohai ohai 'reload' do action :reload end #Reload Ohai after a new user is created ohai 'reload_passwd' do action :nothing plugin 'etc' end user 'daemonuser' do home '/dev/null' shell '/sbin/nologin' system true notifies :reload, 'ohai[reload_passwd]', :immediately end ruby_block 'just an example' do block do # These variables will now have the new values puts node['etc']['passwd']['daemonuser']['uid'] puts node['etc']['passwd']['daemonuser']['gid'] end end