您可以使用为您的特定环境配置正确参数的模板和其他文件。例如,这是我创建 /etc/sysconfig/network-scripts/ifcfg-* 文件的模板:
#
# /etc/sysconfig/network-scripts/ifcfg-<%= @interface %>
#
# Generated by Chef for <%= node['hostname'] %>
#
<%= @interface %>
BOOTPROTO=static
ONBOOT=yes
NM_CONTROLLED=no
<%= @hwaddr %>
<%= @ipaddress %>
<%= @netmask %>
<%= @broadcast %>
<%= @master %>
<%= @slave %>
这是我用来填充变量的配方代码。 Recipe 不知道实际值,只知道基础设施设置,即在一个特定环境中有环境并且有不同的子网:
getMac = {}
# Get "physical" interfaces
node['network']['interfaces'].keys.sort.each do |iface|
if node['network']['interfaces'][iface]['encapsulation'] == 'Ethernet' and
node['network']['interfaces'][iface]['type'] != 'bond'
node['network']['interfaces'][iface]['addresses'].keys.each do |address|
if node['network']['interfaces'][iface]['addresses'][address]['family'] == 'lladdr'
getMac[iface]=address
end
end
end
end
ip = node['network']['address'][node['hostname']]
env = 'dot' + ip.split('.')[2]
netmask = "NETMASK=" + node['network'][env]['netmask']
broadcast = "BROADCAST=" + node['network'][env]['broadcast']
node['network']['interfaces'].keys.sort.each do |iface|
if node['network']['interfaces'][iface]['encapsulation'] == 'Ethernet' and
node['network']['interfaces'][iface]['type'] != 'bond'
# find the non-primary interface, which is not one of eth0, em1, or eno1[0-9]*
# Otherwise, leave IP/netmask/broadcast as assigned above
if !iface.match(/ens16|eno1|eth0|em1/)
ipaddress = ''
netmask = ''
broadcast = ''
else
ipaddress = "IPADDR=" + ip
end
template "/etc/sysconfig/network-scripts/ifcfg-#{iface}" do
source "ifcfg-iface.erb"
mode 0644
owner "root"
group "root"
variables({
:interface => "DEVICE=#{iface}",
:ipaddress => ipaddress,
:netmask => netmask,
:broadcast => broadcast,
:hwaddr => "HWADDR=#{getMac['iface']}",
})
end
end
end
这是环境文件:
override_attributes ({
'network' => {
# one environment can use multiple subnets
'dotX' => {
'broadcast' => "a.b.X.255",
'gateway' => "a.b.X.1",
'netmask' => "255.255.255.0",
},
# all nodes for this environment
'address' => {
'hostname' => "a.b.X.d",
},
},
})