2
votes

I want to override a template in a cookbook wrapper, but the template resource is defined in a provider and not in a recipe. I've been overriding templates in wrappers like this example http://syshero.org/post/67727745605/override-templates-from-third-party-cookbooks-on

include_recipe "rsyslog"

begin
  t = resources(:template => "/etc/rsyslog.conf")
  t.source "rsyslog.conf.erb"
  t.cookbook "example"
rescue Chef::Exceptions::ResourceNotFound
  Chef::Log.warn "could not find template /etc/rsyslog.conf to modify"
end

and that always works as expected.

The cookbook which has a config I'm trying to change is the git_user cookbook https://supermarket.chef.io/cookbooks/git_user

and the point I'm trying to change is in a provider https://github.com/lxmx/chef-git-user/blob/master/providers/default.rb#L30

trying to use the same format as I did for recipes doesn't appear to work?

include_recipe "git_user::data_bag"                                              

def load_current_resource                                                        
    @login = new_resource.login                                                  
    @home  = new_resource.home  || (@login == 'root' ? '/root' : "/home/#{@login}")
end                                                                              

begin                                                                            
    home = @home                                                                 
    r = resources(:template => "#{home}/.ssh/config")                            
    r.cookbook "MY-git_user"                                                    
rescue Chef::Exceptions::ResourceNotFound                                        
    Chef::Log.warn "could not find MY-git_user::data_bag template to override!" 
end  
2
I'm not sure, but could it be that use_inline_resources (defined in the provider) would allow you to do this?StephenKing
ya, its not throwing any errors, it just continues to use the default template in the main cookbook.veilig

2 Answers

2
votes

Not the answer you expect I think and could be not accurate, but turned too long to be a comment:

The lwrp inner resources are compiled and converged at the time the provider is called in the converge phase, they're not processed at compile time, and so they can't be accessed as they don't already exists. (and once the lwrp is done, they are already converged, so you can't modify them at the converge time either).

The use_inline_resources documentation explain this with two tables (compile then converge phase).

So it is not possible to override a resource defined into a provider as it does not appear in the resource list out of the lwrp provider execution itself.

About the use_inline_resource: It makes the notification from inner resources of the lwrp to be triggered by the lwrp resource itself, I see the lwrp resource acting as a proxy for the notifications defined within the provider. This allow resources within the lwrp to notify "recipes" resources.

With or without use_inline_resources the mechanism is the same, inner resources are created and converged at the lwrp convergence time.

0
votes

Ok, the RIGHT thing to do is put in a PR to allow you to specify the overriding cookbook and source in the LWRP as attributes. Anything else is going to be a major hack. However, I believe this will work ( it is really ugly, so please only use it until you can get a PR approved)

some_resource 'myname' do 
  some_attribute lazy do
    home = @home                                                                 
    r = resources(:template => "#{home}/.ssh/config")                            
    r.cookbook "MY-git_user" 
    my_value_for_some_attribute
  end
end

Basically, you are hijacking the lazy attribute to first do your template override at the right time, and then to actually set the value you intended to set.

Not sure if this will even work for you given that you seem to include the LWRP by way of the data_bags recipe from the other cookbook. You may also have to re-implement that recipe locally to allow you to get your hands on the lwrp resource.