Skip to content

Instantly share code, notes, and snippets.

@jordansissel
Created December 9, 2010 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordansissel/735396 to your computer and use it in GitHub Desktop.
Save jordansissel/735396 to your computer and use it in GitHub Desktop.
Exported resource query/collection in ruby puppet dsl
FACTER_fqdn=host1 puppet --certname host1 --node_name fqdn --storeconfigs --dblocation /tmp/puppet.sqlite -e '@@nagios_hostgroup { "mygroup": notes => "Hello"; }'
FACTER_fqdn=host2 puppet --certname host2 --node_name fqdn --storeconfigs --dblocation /tmp/puppet.sqlite -e '@@nagios_hostgroup { "mygroup": notes => "Hello"; }'
# Define a class that provides a bunch of Nagios_hostgroup resources.
# These resources are defined by already-exported Nagios_hostgroups.
# This will also go through and prune out duplicate exports.
require "puppet/rails/resource"
hostclass :happy do
found = Set.new
Puppet::Rails::Resource.find_all_by_restype("Nagios_hostgroup").each do |resource|
# Skip duplicate resource titles
key = [resource.restype, resource.title]
next if found.include?(key)
found << key
# Build a hash of parameters for this resource
params = {}
resource.param_values.each do |pv|
name = pv.param_name.name
params[name] = pv.value
end
# Override the target file for this example
params[:target] = "/tmp/example_nagios_config"
# Now create the resource.
nagios_hostgroup resource.title, params
end
end
node :default do
include "happy"
end
% puppet apply --verbose --storeconfig --dblocation /tmp/puppet.sqlite testpuppet.rb
info: Connecting to sqlite3 database: /tmp/puppet.sqlite
info: Caching catalog for snack.home
info: Applying configuration version '1291931649'
notice: /Stage[main]/Happy/Nagios_hostgroup[mygroup]/ensure: created
snack(~) % cat /tmp/example_nagios_config
# HEADER: This file was autogenerated at Thu Dec 09 13:54:10 -0800 2010
# HEADER: by puppet. While it can still be managed manually, it
# HEADER: is definitely not recommended.
define hostgroup {
notes Hello
hostgroup_name mygroup
}
@michaeldv
Copy link

Try this:
# Build a hash of parameters for this resource
params = Hash[ resource.param_values.map{ |pv| [ pv.param_name.name, pv.value ] } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment