2016-01-24 137 views
0

我有以下廚師食譜,並且我不想在廚師獨奏下融合時運行模板資源。在廚房聚合的時候,它似乎並不服從not_if。它仍然嘗試連接廚師服務器。請幫忙。廚師模板資源not_if獨奏

配方,

template '/etc/hosts' do 
    not_if Chef::Config[:solo] 
    source 'hosts.erb' 
    mode '0644' 
    owner 'root' 
    group 'root' 
    variables({ 
     :nodes => search(:node, 'ipaddress:*') 
      }) 
end 

模板,

... 
<% @nodes.each do |n| -%> 
    <% if (n['fqdn'] && n['ipaddress']) -%> 
     <%= n['ipaddress'] %> <%= n['fqdn'] %> 
    <% end -%> 
<% end -%> 
... 

.kitchen.yml

--- 
driver: 
    name: vagrant 

provisioner: 
    name: chef_solo 

platforms: 
    - name: centos-6.7 
... 

回答

2

傳遞正常值到not_if意味着它被解釋爲命令來運行。你想要的是塊形式:

not_if { Chef::Config[:solo] } 

這就是說,這不是問題。更深層次的問題是,直接在資源體中的所有值都會在編譯時進行評估。您希望使用lazy幫助器來延遲評估,以便只在資源實際運行時才使用它們:

variables(lazy { 
    {:nodes => search(:node, 'ipaddress:*')} 
})