Search code examples
chef-infracookbook

Dynamic resource grouping together recipe actions


I have a set of related operations which I would like to group inside a dynamic resource. Something like this:

# ... somewhere inside my_cookbook/recipes/foo.rb

resource "Initialize git repo inside /etc" do

  not_if { File.exists?('/etc/.git') }

  package "git"

  execute "git init" do 
    cwd '/etc'
  end

  template "/etc/.gitignore" do
    # ...
  end

  bash "initial commit" do 
    # ...
  end

end

Basically I want a quick way to group together a set of operations with:

  • guards like not_if
  • send notifications
  • get extra indentation on my client run logs
  • silence log output inside these blocks (or just change level)

A LWRP is not light weight enough because I do not want to create sets of two files (resource and provider) for a block of code that is only needed once but represent a pattern I want to use all over my recipes.

I could write my own helper for that, but I am wondering if there is something like that already available.


Solution

  • An LWRP is indeed the thing that does this. You could implement some bits of it yourself without that, but the formatter/log would be tricky.