Search code examples
chef-infrachef-recipechef-solotest-kitchen

Chef template resource not_if solo


I have following chef recipe and I don't want to run template resource when it gets converged under chef solo. It does not seem to obey the not_if while running kitchen converge. It still tries to connect the chef server. Please help.

Recipe,

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

Template,

...
<% @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
...

Solution

  • Passing a normal value to not_if means it gets interpreted as a command to run. What you want is the block form:

    not_if { Chef::Config[:solo] }
    

    That said, it isn't the problem. The deeper issue is that all values directly in the resource body get evaluated at compile time. You want to use the lazy helper to delay evaluation so they only get used if the resource is actually run:

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