Chef Infra Language: Checking Platforms
platform?
Use the platform?
helper method to ensure that certain actions are run for specific platforms. The platform?
method will return true if one of the listed parameters matches the node['platform']
attribute that is detected by Ohai during every Chef Infra Client run.
The syntax for the platform?
method is as follows:
platform?('parameter', 'parameter')
where:
parameter
is a comma-separated list, each specifying a platform, such as Red Hat, CentOS, or Fedoraplatform?
method is typically used with anif
,elsif
, orcase
statement that contains Ruby code that is specific for the platform, if detected
platform Values
Parameter | Platforms |
---|---|
aix | IBM AIX |
amazon | Amazon Linux |
arch | Arch Linux |
clearos | ClearOS |
cloudlinux | Cloud Linux OS |
cumulus | NVIDIA Cumulus Linux |
debian | Debian GNU/Linux |
fedora | Fedora |
freebsd | FreeBSD |
gentoo | Gentoo Linux |
linuxmint | Linux Mint |
mac_os_x | macOS |
netbsd | NetBSD |
openbsd | OpenBSD |
openindiana | OpenIndiana |
opensuseleap | openSUSE leap |
pidora | Pidora |
raspbian | Raspberry Pi OS |
scientific | Scientific Linux |
solaris2 | Oracle Solaris |
suse | SUSE Enterprise Linux Server. |
ubuntu | Ubuntu Linux |
windows | Microsoft Windows |
xenserver | Citrix XenServer |
Examples
Installing the cron package on Debian systems
package 'cron' if platform?('debian')
Deleting a file on Red Hat and Debian systems
if platform?('redhat', 'debian')
file '/etc/some_config' do
action :remove
end
end
Installing the correct Firefox package
The following example shows how an if statement can be used with the
platform?
method in the Chef Infra Language to run code specific to Microsoft
Windows. The code is defined using the ruby_block resource:
if platform?('windows')
chocolatey_package 'firefox'
else
package 'firefox'
end
platform_family?
Use the platform_family?
method to ensure that certain actions are run for specific platform family. The platform_family?
method will return true if one of the listed parameters matches the node['platform_family']
attribute that is detected by Ohai during every Chef Infra Client run.
The syntax for the platform_family?
method is as follows:
platform_family?('parameter', 'parameter')
where:
'parameter'
is a comma-separated list, each specifying a platform family, such as Debian, or Red Hat Enterprise Linuxplatform_family?
method is typically used with anif
,elsif
, orcase
statement that contains Ruby code that is specific for the platform family, if detected
platform_family Values
Parameter | Platforms |
---|---|
aix | aix platform. |
alpine | alpine platform. |
amazon | amazon platform. |
arch | arch , manjaro , and antergos platforms. |
debian | debian , ubuntu , linuxmint , raspbian , cumulus , kali , pop platforms. |
fedora | fedora , pidora , and arista_eos platform |
freebsd | freebsd platform |
gentoo | gentoo platform |
mac_os_x | mac_os_x platform |
netbsd | netbsd platform |
openbsd | openbsd platform |
openindiana | openindiana platform |
rhel | redhat , centos , oracle , scientific , xenserver , clearos , bigip , parallels , xcp , and ibm_powerkvm platforms |
solaris2 | solaris2 platform |
sles | opensuse_leap , suse , and sled platforms |
windows | windows platform |
Examples
For example:
platform_family?('gentoo')
or:
platform_family?('slackware', 'suse', 'arch')
Use a specific binary for a specific platform
The following is an example of using the platform_family?
method in
the Chef Infra Language to create a variable that can be used with other
resources in the same recipe. In this example, platform_family?
is
being used to ensure that a specific binary is used for a specific
platform before using the remote_file resource to download a file
from a remote location, and then using the execute resource to
install that file by running a command.
if platform_family?('rhel')
pip_binary = '/usr/bin/pip'
else
pip_binary = '/usr/local/bin/pip'
end
remote_file "#{Chef::Config[:file_cache_path]}/distribute_setup.py" do
source 'http://python-distribute.org/distribute_setup.py'
mode '0755'
not_if { ::File.exist?(pip_binary) }
end
execute 'install-pip' do
cwd Chef::Config[:file_cache_path]
command <<-EOF
# command for installing Python goes here
EOF
not_if { ::File.exist?(pip_binary) }
end
where a command for installing Python might look something like:
#{node['python']['binary']} distribute_setup.py
#{::File.dirname(pip_binary)}/easy_install pip
value_for_platform
Use the value_for_platform
method in a recipe to select a value based on the node['platform']
and node['platform_version']
attributes. These values are detected by Ohai during every Chef Infra Client run.
The syntax for the value_for_platform
method is as follows:
value_for_platform( ['platform', ...] => { 'version' => 'value' } )
where:
'platform', ...
is a comma-separated list of platforms, such as Red Hat, openSUSE, or Fedoraversion
specifies the version of that platform- Version constraints—
>
,<
,>=
,<=
,~>
—may be used withversion
; an exception is raised if two version constraints match; an exact match will always take precedence over a match made from a version constraint value
specifies the value that will be used if the node’s platform matches thevalue_for_platform
method
When each value only has a single platform, use the following syntax:
value_for_platform(
'platform' => { 'version' => 'value' },
'platform' => { 'version' => 'value' },
'platform' => 'value'
)
When each value has more than one platform, the syntax changes to:
value_for_platform(
['platform', 'platform', ... ] => {
'version' => 'value'
},
)
Operators
The following operators may be used:
Operator | Description |
---|---|
= | equal to |
> | greater than |
< | less than |
>= | greater than or equal to; also known as "optimistically greater than", or "optimistic" |
<= | less than or equal to |
~> | approximately greater than; also known as "pessimistically greater than", or "pessimistic" |
Examples
The following example will set package_name
to httpd
for the Red Hat platform and to apache2
for the Debian platform:
package_name = value_for_platform(
['centos', 'redhat', 'suse', 'fedora' ] => {
'default' => 'httpd'
},
['ubuntu', 'debian'] => {
'default' => 'apache2'
}
)
The following example will set package
to apache-couchdb
for OpenBSD platforms, dev-db/couchdb
for Gentoo platforms, and couchdb
for all other platforms:
package = value_for_platform(
'openbsd' => { 'default' => 'apache-couchdb' },
'gentoo' => { 'default' => 'dev-db/couchdb' },
'default' => 'couchdb'
)
The following example shows using version constraints to specify a value based on the version:
value_for_platform(
'os1' => { '< 1.0' => 'less than 1.0',
'~> 2.0' => 'version 2.x',
'>= 3.0' => 'greater than or equal to version 3.0',
'3.0.1' => '3.0.1 will always use this value' }
)
value_for_platform_family
Use the value_for_platform_family
method in a recipe to select a value based on the node['platform_family']
attribute. This value is detected by Ohai during every Chef Infra Client run.
The syntax for the value_for_platform_family
method is as follows:
value_for_platform_family( 'platform_family' => 'value', ... )
where:
'platform_family' => 'value', ...
is a comma-separated list of platforms, such as Fedora, openSUSE, or Red Hat Enterprise Linuxvalue
specifies the value that will be used if the node’s platform family matches thevalue_for_platform_family
method
When each value only has a single platform, use the following syntax:
value_for_platform_family(
'platform_family' => 'value',
'platform_family' => 'value',
'platform_family' => 'value'
)
When each value has more than one platform, the syntax changes to:
value_for_platform_family(
['platform_family', 'platform_family', 'platform_family', 'platform_family' ] => 'value',
['platform_family', 'platform_family'] => 'value',
'default' => 'value'
)
The following example will set package
to httpd-devel
for the Red Hat Enterprise Linux, Fedora, and openSUSE platforms and to apache2-dev
for the Debian platform:
package = value_for_platform_family(
['rhel', 'fedora', 'suse'] => 'httpd-devel',
'debian' => 'apache2-dev'
)
Was this page helpful?