Pass aggregate hash to class to declare several defined types

39 views
Skip to first unread message

Nathan Nobbe

unread,
Oct 30, 2013, 6:58:44 PM10/30/13
to puppet...@googlegroups.com
Hi,

A bit of background, I'm trying to move from node inheritance to hiera. With node inheritance I could declare defined types at several levels, for example, say for all of preproduction I'd like a yum repository

manifests/pre-prod.pp
class preprod {
  yumrepo { 'preprod-repo': ... }
}

Then add other repos in various sub classes where other specialized yumrepo(s) are declared
manifests/qa.pp
class qa inherits preprod {
  yumrepo { 'qa': ... }
}

manifests/dev.pp
class dev inherits preprod {
  yumrepo {'dev': ... }
}

I'm having a hard time getting this to map to hiera cleanly. I'd like to have the data in the hierarchy something like this

preprod.yaml
yumrepo::name: preprod-repo
...

qa.yaml
yumrepo::name: qa-repo
...

My initial thought was to use hiera_include('classes'), but that doesn't work for defined types. What folks on superuser.com have suggested is to define a wrapper class for *each* declaration of a defined type. Something like

class preprod_repo {
  yumrepo { 'preprod-repo': ... }
}

class qa_repo {
  yumrepo { 'qa-repo': ... }
}

Then in the yaml

preprod.yaml
classes:
 - preprod_repo
...

qa.yaml
classes:
 - qa_repo
...

I really don't want to define a class for every single type instantiation. What I'd prefer would be a single class that could capture a merged hash from all the yaml files and write out all the type declarations, but I'm having trouble with the implementation. From the yaml side, I've got what I want, something like this

preprod.yaml
classes:
 - yumrepo_hiera

yumrepo_hiera: {
  'preprod_repo' : {
     # attributes for preprod yum repo
  }
}

qa.yaml
classes:
 - yumrepo_hiera

yumrepo_hiera: {
  'qa_repo' : {
     # attributes for qa yum repo
  }
}

Now I'm passing an aggregate data object with all the data needed to the yumrepo_hieara class. Problem is how to write out all those type declarations. Since there's no looping in RDL. I know it's deprecated, but the Ruby DSL seems to be my best bet, yet I can't get the hash from hiera passed in.., I'm getting this error when I run the agent

undefined method `merge' for :yumrepo_hiera:Symbol

Here's what I've got so far, bear in mind I know nothing of Ruby, but hopefully you can get the idea

hostclass :yumrepo_hiera, :yumrepo_hiera do
   @yumrepo_hiera.each do |name, repo|
        yumrepo( :name,
                 repo[:descr],
                 # other parameters to yumrepo type...
         )       
   end
end

Can someone help me get off the ground with this Ruby DSL, or show me a better way in general, one where I don't have to write a class per each node declaration?

thanks,

-nathan

Nathan Nobbe

unread,
Oct 31, 2013, 1:17:43 AM10/31/13
to puppet...@googlegroups.com
Solved it with create_resources.
Reply all
Reply to author
Forward
0 new messages