Passing hash as parameters to manifest

6,406 views
Skip to first unread message

treydock

unread,
Aug 11, 2011, 5:00:43 PM8/11/11
to Puppet Users
I have a module for backuppc, and am trying to pass a hash to a define
to create a single script and the necessary directories. What I can't
seem to figure out how to do is have this hash's values be used to
create files / directories and also populate a template.

Here's the hash...


$backuppc_db_dumps = {
redmine => {
'backup_dir' => '/var/www/rails/redmine',
'databases' => 'redmine',
'dump_dir' => '/backups/misc-sqldumps',
},
general => {
'backup_dir' => '/etc',
'databases' => 'mysql',
'dump_dir' => '/backups/misc-sqldumps',
},
}


I have successfully used that to with a template to generate a script,
but am unsure how to pass those values to a define in order to ensure
the "dump_dir" exists.

After the above variable I added

backuppc::sqldump { $backuppc_db_dumps: }

Here's the define ...

define backuppc::sqldump () {

file {
"$name[dump_dir]":
ensure => directory,
owner => 'root',
group => 'root',
mode => '0770',
}
}

Is this something that's even possible? The error I get doesn't make
any sense to me...

err: Could not retrieve catalog from remote server: Error 400 on
SERVER: Invalid tag "generaldump_dir/backups/misc-
sqldumpsdatabasesmysqlbackup_dir/etcredminedump_dir/backups/redmine-
sqldumpsdatabasesredminebackup_dir/var/www/rails/redmine" at /etc/
puppet/modules/backuppc/manifests/definitions/sqldump.pp:9 on node

Thanks
- Trey

Nan Liu

unread,
Aug 11, 2011, 6:59:36 PM8/11/11
to puppet...@googlegroups.com
On Thu, Aug 11, 2011 at 4:00 PM, treydock <trey...@gmail.com> wrote:
> I have a module for backuppc, and am trying to pass a hash to a define
> to create a single script and the necessary directories.  What I can't
> seem to figure out how to do is have this hash's values be used to
> create files / directories and also populate a template.
>
> Here's the hash...
>
>
>    $backuppc_db_dumps =  {
>        redmine => {
>                'backup_dir'    => '/var/www/rails/redmine',
>                'databases'     => 'redmine',
>                'dump_dir'      => '/backups/misc-sqldumps',
>        },
>        general => {
>                'backup_dir'    => '/etc',
>                'databases'     => 'mysql',
>                'dump_dir'      => '/backups/misc-sqldumps',
>        },
>    }
>
>
> I have successfully used that to with a template to generate a script,
> but am unsure how to pass those values to a define in order to ensure
> the "dump_dir" exists.
>
> After the above variable I added
>
> backuppc::sqldump { $backuppc_db_dumps: }

You are passing a hash as the resource title, a resource title is
either a string or array of string.

> Here's the define ...
>
> define backuppc::sqldump () {
>
>    file {
>        "$name[dump_dir]":
>            ensure  => directory,
>            owner   => 'root',
>            group   => 'root',
>            mode    => '0770',
>    }
> }
>
> Is this something that's even possible?  The error I get doesn't make
> any sense to me...

Not in the current form, what you are looking for is probably best
described here:
http://projects.puppetlabs.com/issues/8670

However a small change should allow this to work. (disclaimer,
untested, but I've done something similar).

define backuppc::sqldump ($var) {
$value = $var[$name]
   file {
       "$value[dump_dir]":


           ensure  => directory,
           owner   => 'root',
           group   => 'root',
           mode    => '0770',
   }

}

backuppc::sqldump { ['redmine', 'general']:
var =>$backuppc_db_dumps,
}

If you have a functions that gets an array of the first level hash
keys, you can use that instead of specifying redmine, general.

HTH,

Nan

treydock

unread,
Aug 11, 2011, 8:19:16 PM8/11/11
to Puppet Users
Looking up the use of create_resources which is mentioned in the bug
you linked, looks like it's available only in 2.7.x. I'm currently
running 2.6.9, but may be worth upgrading for.

I tried you suggestion, but get this error...

err: Could not retrieve catalog from remote server: Error 400 on
SERVER: Invalid tag "dump_dir/backups/redmine-
sqldumpsdatabasesredminebackup_dir/var/www/rails/redmine" at /etc/
puppet/modules/backuppc/manifests/definitions/sqldump.pp:11 on
node ...


Also I am not sure what you mean by "functions that get an array of
first level hash keys".

Thanks
- Trey

On Aug 11, 5:59 pm, Nan Liu <n...@puppetlabs.com> wrote:

Nan Liu

unread,
Aug 11, 2011, 10:11:31 PM8/11/11
to puppet...@googlegroups.com
On Thu, Aug 11, 2011 at 7:19 PM, treydock <trey...@gmail.com> wrote:
> Looking up the use of create_resources which is mentioned in the bug
> you linked, looks like it's available only in 2.7.x.  I'm currently
> running 2.6.9, but may be worth upgrading for.

Pretty sure you can be backport to 2.6 since it's just a function:

https://github.com/puppetlabs/puppetlabs-create_resources

> I tried you suggestion, but get this error...
>
> err: Could not retrieve catalog from remote server: Error 400 on
> SERVER: Invalid tag "dump_dir/backups/redmine-
> sqldumpsdatabasesredminebackup_dir/var/www/rails/redmine" at /etc/
> puppet/modules/backuppc/manifests/definitions/sqldump.pp:11 on
> node ...

Can't say for sure without more code, but this simple example should
demonstrate how this works:

$myhash = {
var1 => {
message => 'hi'
},
var2 => {
message => 'bye'
}
}

define my_notify ($var) {
notify { $name:
message => $var[$name]['message']
}
}

my_notify { ['var1', 'var2']:
var => $myhash
}

> Also I am not sure what you mean by "functions that get an array of
> first level hash keys".

I mean if you need this to work with any hash without manually
providing an array of resource titles write a custom puppet function
that returns hash keys as a custom puppet function.

Thanks,

Nan

treydock

unread,
Aug 11, 2011, 11:45:39 PM8/11/11
to Puppet Users


On Aug 11, 9:11 pm, Nan Liu <n...@puppetlabs.com> wrote:
Got it working now!

Your first example would have worked, but I had put double quotes
around the file resource name which was breaking the module. In case
anyone runs into something similar, here's exactly what I used to make
this definition work...


My node definition contains...


$backuppc_db_dumps = {
'redmine' => {
backup_dir => '/var/www/rails/redmine',
databases => 'redmine',
dump_dir => '/backups/redmine-sqldumps',
},
'general' => {
backup_dir => '/etc',
databases => 'mysql',
dump_dir => '/backups/misc-sqldumps',
},
}

backuppc::sqldump { [ 'redmine', 'general' ]:
backup_db => $backuppc_db_dumps,
}


This is the define ...

define backuppc::sqldump ($backup_db) {

file {
$backup_db[$name]['dump_dir']:
ensure => directory,
owner => 'root',
group => 'root',
mode => '0770';
}
}

Thanks Nan for your help!

- Trey

Reinaldo Lima

unread,
Jan 16, 2014, 2:24:20 PM1/16/14
to puppet...@googlegroups.com
Nan Liu,

this help me A LOT,

thank you so much!
Reply all
Reply to author
Forward
0 new messages