您可以使用模板來爲您的特定環境使用正確的參數進行配置和其他文件。例如,這是我創建的/ etc/sysconfig/network中的腳本/用於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 %>
這裏是我的食譜代碼來填充變量。配方有沒有關於實際值的想法,只是對基礎設施的設置,即有環境,有一個特定的環境中不同的子網:
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",
},
},
})
如果你真的只是想要更改文件的一小部分,最好用'.erb'文件完成。正如我在答覆中提到的,這可能非常混亂。我認爲將您的項目代碼(如html,js,服務器腳本文件)放在Chef本身中是個不錯的主意;我更喜歡使用源代碼控制系統(比如git或svn)。 –
我感謝您給我的所有幫助,感謝您指引我朝着正確的方向發展。 – gsteigerwald
順便說一下,我使用git進行源代碼控制,但需要有js,php等的一些部分。文件在創建哪個環境時是動態的 – gsteigerwald