Chef fails on overwriting a wrapper template

I have two wrapper cookbooks and a community cookbook apache2. The 1st wrapper cookbook is our core_dispatcher wrapper that uses apache2 widely used for all clients. The 2nd wrapper is for a particular client client_dispatcher cookbook. I want to make a change to only one client so I am using the 2nd client_dispatcher. When I ran chef-client on the server the resource gets created and I am able to see my changes but chef fails.

I am not sure why it’s failing on line 2 if that’s from the apache2 cookbook. I am using chef 12 version.

This is the error I get

    Recipe: client_dispatcher::default
 * template[/etc/httpd/sites-available/testcom.conf] action create

    ================================================================================
    Error executing action `create` on resource 'template[/etc/httpd/sites-available/testcom.conf]'
    ================================================================================

Chef::Mixin::Template::TemplateError
------------------------------------
undefined method `[]' for nil:NilClass

Resource Declaration:
---------------------
# In /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/dsl/declare_resource.rb

158:         declare_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
159:       end

Compiled Resource:
------------------
# Declared in /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/dsl/declare_resource.rb:158:in `rescue in edit_resource'

template("/etc/httpd/sites-available/testcom.conf") do
  action [:create]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  source "dispatcher-vhost.conf.erb"
  cookbook "client_dispatcher"
  declared_type :template
  cookbook_name "client_dispatcher"
  recipe_name "default"
  mode nil
  owner nil
  group nil
  path "/etc/httpd/sites-available/testcom.conf"
  verifications []
end

Template Context:
-----------------
on line #2
  1: <% if node['apache']['version'] && node['apache']['version'] != '2.4' -%>
  2: NameVirtualHost *:<%= @params[:server_port] %>
  3: <% end -%>
  4: <VirtualHost *:<%= @params[:server_port] %>>
  5:   ServerAdmin <%= node['apache']['server_admin'] %>

Based on your output, the template expects a variables hash passed to it with a “params” hash with a key/value for :server_port. I don’t see this being passed from your recipe, hence the undefined method on nil.

See https://docs.chef.io/resource_template.html for examples.

—Jp

1 Like

Hi @dreamnite thanks for your reply. I’m still a beginner in chef. I tried adding the variable server_port => 80 but still fails is this what you meant I need to do?

My recipe:

include_recipe 'core_dispatcher::default'


edit_resource(:template, '/etc/httpd/sites-available/testcom.conf') do
    source 'dispatcher-vhost.conf.erb'
    cookbook 'client_dispatcher'
    variables(:server_port => 80)
end 

chef-client failed output

Recipe: client_dispatcher::default
* template[/etc/httpd/sites-available/testcom.conf] action create

================================================================================
Error executing action `create` on resource 'template[/etc/httpd/sites-available/testcom.conf]'
================================================================================

Chef::Mixin::Template::TemplateError
------------------------------------
undefined method `[]' for nil:NilClass

Resource Declaration:
---------------------
# In /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/dsl/declare_resource.rb

158:         declare_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
159:       end

Compiled Resource:
------------------
# Declared in /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/dsl/declare_resource.rb:158:in `rescue in edit_resource'

template("/etc/httpd/sites-available/testcom.conf") do
  action [:create]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  source "dispatcher-vhost.conf.erb"
  cookbook "client_dispatcher"
  variables {:server_port=>80}
  declared_type :template
  cookbook_name "client_dispatcher"
  recipe_name "default"
  mode nil
  owner nil
  group nil
  path "/etc/httpd/sites-available/testcom.conf"
  verifications []
end

Template Context:
-----------------
on line #2
  1: <% if node['apache']['version'] && node['apache']['version'] != '2.4' -%>
  2: NameVirtualHost *:<%= @params[:server_port] %>
  3: <% end -%>
  4: <VirtualHost *:<%= @params[:server_port] %>>
  5:   ServerAdmin <%= node['apache']['server_admin'] %>

My chef-client created the file in /etc/httpd/sites-available/testcom.conf but chef still fails.

NameVirtualHost *:80
<VirtualHost *:80>
 ServerAdmin email name
 ServerName some servername here
 ServerAlias  some alias
 DocumentRoot /var/www/cache/publish/testcom

<Directory /var/www/cache/publish/testcom>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
     ...other configs
</VirtualHost>
1 Like

Adding the params to the recipe worked.
variables(:params => {:server_port => 80})