厨师:如何修改confg文件才能重新启动节点

环境:aws opsworks厨师11.10和Ubuntu 14.04。

我正在使用大厨食谱来更新aws opsworks ec2节点上的/etc/dhcp/dhclient.conf文件,以便将我的自定义dnssearch后缀添加到/etc/resolv.conf文件中的search行。

如果file.insert_line_if_no_match更新文件,我怎么才能让我的配方重新启动节点? 我显然不希望每次配方运行时重新启动节点。

在我的node['opsworks']['stack']['name']下面的代码片断node['opsworks']['stack']['name']值就像a.dev.mydomain.com

 ruby_block "add custom dns domain search suffix" do block do file = Chef::Util::FileEdit.new("/etc/dhcp/dhclient.conf") file.insert_line_if_no_match("/append domain-search/", "append domain-search \"#{node['opsworks']['stack']['name']}\";") file.write_file end end 

上面的代码片段将最后一行添加到/etc/dhcp/dhclient.conf

 # Configuration file for /sbin/dhclient, which is included in Debian's # dhcp3-client package. # option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; send host-name = gethostname(); request subnet-mask, broadcast-address, time-offset, routers, domain-name, domain-name-servers, domain-search, host-name, dhcp6.name-servers, dhcp6.domain-search, netbios-name-servers, netbios-scope, interface-mtu, rfc3442-classless-static-routes, ntp-servers, dhcp6.fqdn, dhcp6.sntp-servers; append domain-search "a.dev.mydomain.com"; 

重新启动后,/ /etc/dhcp/dhclient.conf被修改如下:

 # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 172.16.0.23 search ec2.internal a.dev.mydomain.com 

注意:我认为这样做太冒险了,试着用sudo dhclient -r; sudo dhclient来实现这个设置sudo dhclient -r; sudo dhclient sudo dhclient -r; sudo dhclient文件更新后,但我很想听听有没有人得到这种types的更新工作,而无需重新启动。

我强烈build议你find一种方法来做到这一点,而无需重新启动机器。 例如,我在Centos上做了类似的事情,这是通过sudo service network restart 。 尽pipe如此,无论您是重新启动机器还是只使用一项服务,您都将使用相同的模式。

正确的方式

正确的方法是使用自定义资源/提供者。 在这种情况下(社区里可能已经有一个),你可以编写一个读取dhclient.conf文件的LWRP,并且只在需要修改时修改它。 如果文件被更新,它会设置updated_by_last_action 。 在这一点上,你可以使用serviceexecute资源来重新启动服务/机器。 重新启动资源将订阅对LWRP资源的更改。

几乎同样酷的方式

或者,你做这样的事情:

 execute "add custom dns domain search suffix" do command "echo 'append domain-search \"#{node['opsworks']['stack']['name']}\";' >> /etc/dhcp/dhclient.conf" not_if { ::File.open('/etc/dhcp/dhclient.conf').read() =~ /append domain-search/ } end execute 'restart machine' do command 'shutdown immediate -r' action :nothing subscribes :run, 'execute[add custom dns domain search suffix]' end 

非常感谢Tejay Cardon,你的代码就像一个魅力(我保证我会考虑写一个LWRP)。

FTR下面的代码适用于aws ec2 ubuntu 14.04版本, 无需重启 。 我甚至可以做一个sudo ifdown eth0 && sudo ifup eth0从一个SSH会话,并没有断开连接。

 execute "add custom dns domain search suffix" do command "echo 'append domain-search \"#{node['opsworks']['stack']['name']}\";' >> /etc/dhcp/dhclient.conf" not_if { ::File.open('/etc/dhcp/dhclient.conf').read() =~ /append domain-search/ } end execute 'bounce eth0' do command 'sudo ifdown eth0 && sudo ifup eth0' action :nothing subscribes :run, 'execute[add custom dns domain search suffix]' end 

由于某种原因 sudo service networking restartsudo /etc/init.d/networking restart不再工作后,Ubuntu 13.04和我不这样做在台式机或sudo service network-manager restart可能已经工作。