How to add “ensure” arguments to puppet definitions

puppet

I have a puppet definition that runs an exec type to create a mysql user that looks like this:

define mysql::mysql-user ($user, $host="$mysql_subnet", $passwd="", $ensure="present") {
    exec { "mysql -e \"CREATE USER '$user'@'$host' IDENTIFIED BY PASSWORD '$passwd'\"":
        path    => "/usr/bin:/bin",
        unless => "mysql -e \"SELECT Host,User FROM mysql.user WHERE Host='$host' AND User='$user'\" | grep $user",
        require => Service[ mysql ], 
    }
}

So, I want to make the $ensure argument work properly, calling a completely different exec to drop the user. What's the best way to go about this?

I've tried the following setup:

define mysql::mysql-user ($user, $host="$mysql_subnet", $passwd="", $ensure="present") {
    case $ensure {
        present:    { include add-user }
        absent:     { include remove-user }
    }
}

class mysql::add-user {
    exec { ... }
}

class mysql::remove-user {
    exec { ... }
}

but this fails because my grants definition (not shown) is now somehow no longer dependent on mysql-user running first.

Best Answer

define blah($ensure = present) {
    if $ensure == "present" {
        exec { do_something_to_make_it_be_here: }
    } else {
        exec { do_something_to_make_it_not_be_here: }
    }
}

For more than just ensure present/absent, you can switch to a case in the same vein. Don't go to subclasses, because as you say they lose dependency information.

Related Topic