Array use as loop-type construct?

104 views
Skip to first unread message

Bret Wortman

unread,
Jul 23, 2013, 1:32:43 PM7/23/13
to puppet...@googlegroups.com
I'm trying to use a puppet manifest to set up a series of backup jobs on servers which are each running a variety of mysql databases. My manifest currently looks something like this, which almost works:

class backups () {

    Cron {
        ensure => present,
        user => root,
    }

    $remote_dest = ['zx2:/data/src/backups','zx3:/data/backups']

    if $hostname == "www" {
        $databases = ['phpbb3','wikidb','rt4','wordpress']
        $bkp_dest = "/data/backup"
    elsif...
    :
    }

    cron { $databses:
        command => "mysqldump --user admin $databases | gzip > mysql-$databases-`date +%Y%m%d`.gz",
        hour => 1,
        minute => 20,
    }

    cron { $remote_dest:
        command => "rsync -arzv $bkp_dest $remote_dest",
        hour => 4,
        minute => 40,
    }
}

I get the right number of cron jobs built, but when the array variables are quoted, I get the contents all concatenated together. What I'd like is to simulate a "for x in array" kind of construct. Is that possible using puppet? I'd rather not have to build this and specify each job by hand, which seems really clunky given the elegance of the tool. I'm sure I'm just not seeing something.

Thanks!


Bret

Bret Wortman

unread,
Jul 24, 2013, 8:29:35 AM7/24/13
to puppet...@googlegroups.com
For completeness, I solved this myself by using a template instead of trying to do this all within a manifest:

#!/bin/sh

<% databases.each do |db| -%>
<%= "mysqldump --user admin #{db} | gzip > mysql-#{db}-`date +%Y%m%d`.gz" %>
<% end -%>

<% remote_dest.each do |rd| -%>
<%= "rsync -arzv #{bkp_dest} #{rd}" %>
<% end -%>

And the manifest then simplifies to:

file { "backup-script":
    ensure => present,
    path => $mysql-backup-script,
    content => template('backups/backup-mysql.erb'),
    mode => 744,
    owner => "root",
    group => "root",
}

cron { "backup-mysql":
    command => $mysql-backup-script,
    hour => 1,
    minute => 20,
    require => File['backup-script'],

jcbollinger

unread,
Jul 29, 2013, 1:19:12 PM7/29/13
to puppet...@googlegroups.com


On Tuesday, July 23, 2013 12:32:43 PM UTC-5, Bret Wortman wrote:
I'm trying to use a puppet manifest to set up a series of backup jobs on servers which are each running a variety of mysql databases. My manifest currently looks something like this, which almost works:

class backups () {

    Cron {
        ensure => present,
        user => root,
    }

    $remote_dest = ['zx2:/data/src/backups','zx3:/data/backups']

    if $hostname == "www" {
        $databases = ['phpbb3','wikidb','rt4','wordpress']
        $bkp_dest = "/data/backup"
    elsif...
    :
    }

    cron { $databses:
        command => "mysqldump --user admin $databases | gzip > mysql-$databases-`date +%Y%m%d`.gz",
        hour => 1,
        minute => 20,
    }

    cron { $remote_dest:
        command => "rsync -arzv $bkp_dest $remote_dest",
        hour => 4,
        minute => 40,
    }
}

I get the right number of cron jobs built, but when the array variables are quoted, I get the contents all concatenated together.


Yes, that is what you get when you convert an array to a string.

 
What I'd like is to simulate a "for x in array" kind of construct. Is that possible using puppet? I'd rather not have to build this and specify each job by hand, which seems really clunky given the elegance of the tool. I'm sure I'm just not seeing something.


There is the template approach you discovered, when it is applicable.  The more general approach is to use a defined type in conjunction with Puppet's shorthand for declaring multiple resources based on an array of their titles.  You're example code is heading in the right direction.  Here's how a working version might look:

define backups::database () {
  cron { "backup_database_${title}":

    ensure  => present,
    user    => root,
    command => "mysqldump --user admin ${title} | gzip > mysql-${title}-`date +%Y%m%d`.gz",
    hour    => 4,
    minute  => 40,
  }
}

define backups::remote () {
  cron { "backup_remote_dest_${title}":

    ensure  => present,
    user    => root,
    command => "rsync -arzv ${backups::bkp_dest} ${title}",
    hour    => 1,
    minute  => 20,
  }
}

class backups {
  # ...
  # Set variables, including $databases, $bkp_dest, and $remote_dest
  # ...

  backups::database { $databases: }
  backups::remote { $remote_dest: }
}

Note the use of the automatic $title variable inside the definitions to refer to the title of the defined type instance.  You can declare other parameters to the definition, too, but as used in the example, all instances would get the same values for those parameters.  There are ways to be cleverer about that sort of thing, once you have a hold of what's going on.


John

Bret Wortman

unread,
Jul 29, 2013, 2:25:07 PM7/29/13
to puppet...@googlegroups.com
Oooooh. I've never really grokked the defined type stuff. I can see now I need to dig in and make sense of it.

Thanks, John!

--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/rm1wx3s4ZiE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages