Ask Your Question
0

How to create resource definition dynamically?

asked 2014-08-18 01:25:54 -0500

zamoose gravatar image

I'm trying to allow users to specify a custom resource type's configuration using JSON files that are imported via Hiera. I'd like to have the resource use all the default values unless the user's JSON explicitly specifies a particular attribute, but I'm at a bit of a loss as to how to accomplish this.

Here's a bit of pseudo Puppet code to show what I mean:

$sites = hiera_hash('sites')

$sites.each |$site, $props| {
    trt::install {
        $site:
        $props.each |$prop, $value| {
            $prop => $value
        }
    }
}

This completely doesn't work. Here's an example JSON file:

{
    "sites": {
        "unittest.dev": {
            "install_path": "/var/www/unittest.dev",
            "db_user": "wordpress",
            "db_pass": "wordpress",
            "db_name": "wordpress",
        },
        "wptest.dev": {
            "install_path": "/var/www/wptest.dev",
            "db_user": "wordpress2",
            "db_pass": "wordpress2",
            "db_name": "wordpress2",
        },
        "multitest.dev": {
            "install_path": "/var/www/multitest.dev",
            "db_user": "wordpress_multi",
            "db_pass": "wordpress_multi",
            "db_name": "wordpress_multi",
            "multisite": "true"
        }
    }
}

If you look at the JSON above, you'll note I only specified "multisite" for the last site. Ideally, I would be able to avoid having to specify those sorts of attributes for every site and only invoke them when they deviate from the default.

Anyone have any brilliant ideas?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-08-18 07:35:23 -0500

llowder gravatar image

I would recommend that you use create_resources(). You can find the docs here.

TL;DR version:

create_resources('my_type', $my_value_hash, $my_defaults_hash)

The variable $my_value_hash would be the hash that your end users supply, and $my_defaults_hash would be the hash you supply.

Since you want users to be able to specify this resource, you'll want to use hiera_hash() to pull in $my_value_hash and a normal hiera() call ( either explicit or via automatic parameter lookups) for the $my_defaults_hash.

edit flag offensive delete link more

Comments

create_resources is the right way to do that. I prefer to define the defaults in the my_type, where you're more flexible.

khaefeli gravatar imagekhaefeli ( 2014-08-18 10:05:35 -0500 )edit

I like to specify the defaults in the hash, because the you can get a more consistent usage when you are using a this with a mix of custom and built in types. It also allows you to easily have different sets of defaults based on your hiera hierarchy

llowder gravatar imagellowder ( 2014-08-18 10:25:04 -0500 )edit

One question: Does Hiera handle translating a JSON hash into a Puppet-friendly hash definition by default?

zamoose gravatar imagezamoose ( 2014-08-18 10:50:51 -0500 )edit

if you're hiera integration etc is working: yes for sure, you've to care about sequences etc, if you've other datasources. https://docs.puppetlabs.com/hiera/1/data_sources.html

khaefeli gravatar imagekhaefeli ( 2014-08-18 11:00:45 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-18 01:25:54 -0500

Seen: 900 times

Last updated: Aug 18 '14