Puppet hash create file

puppetresources

Sooo i have this hash in app1's puppet manifest

$applicaton = 'app1'

daemontools::build {
    $application:
        path     => "/opt/supervise/${application}"
        envvars  => {
            'ENVIRONMENT'              => $location,
            'SERVICE_USER'             => $application_user,
            'SERVICE_PORT'             => $gunicorn_port,
            'SERVICE_IP'               => $gunicorn_ip,
            'ADDITIONAL_PARAMS'        => "--workers $processorcount",
            'DJANGO_SETTINGS_MODULE'   => "${application}.settings",
        }
}

and this hash in my app2's puppet manifest

$applicaton = 'app2'

daemontools::build {
    $application:
        path     => "/opt/supervise/${application}"
        envvars  => {
            'ENVIRONMENT'              => $location,
            'SERVICE_USER'             => $application_user,
            'SERVICE_PORT'             => $gunicorn_port,
            'SERVICE_IP'               => $gunicorn_ip,
            'ADDITIONAL_PARAMS'        => "--workers $processorcount",
            'DJANGO_SETTINGS_MODULE'   => "${application}.settings",
        }
}

which are both passed to daemontools::build ( along with a path )

define daemontools::build (
$envvars = {},
$path
){
    $env_names = keys($envvars)
    daemontools::envfile { $env_names:
        path   => $path,
        value  => $envvars
    }
}
define daemontools::envfile($path, $value) {
    file { "/${path}/envdir/${name}" :
        mode    => 0644,
        content => "${value[$name]}";
    }
}

which results in

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: Daemontools::Envfile[SERVICE_IP] is already declared in file daemontools/manifests/build.pp:53; cannot redeclare at daemontools/manifests/build.pp:53

How can i make it so that i wont get duplicate resource declarations?

Best Answer

In the creation of your daemontools::envfile object, you should add a unique identifier to the $name of the object.

daemontools::envfile { $something_unique$env_names:
    path   => $path,
    value  => $envvars
}

By default, $name is each of your $env_names. When you create the second object with the same set of keys, it causes many duplicates. FQDN or something equally unique could be useful for lookup.