Ask Your Question
0

Check if mysql config file exists and do something

asked 2014-07-23 10:06:49 -0500

updated 2014-07-23 10:07:18 -0500

Hi!

I'm using the official puppetlabs mysql module. now I want to autogenerate the root password without the loosing the possibility to set it in hiera.

to generate the password I'm using:

class { 'mysql::server':
         root_password    => generate("/bin/sh", "-c", "/usr/bin/pwgen 30 1| tr -d '\n' "),
      }

now I need to check, if the password is already set. I think the official module does not provide such an option. so I tried it with:

$mycnf = file ("/root/.my.cnf")
if 'password' in $mycnf {
        #inlcude the mysql::server class without setting a root password
}

but that's a bad option, because if the file does not exist:

Error: Could not find any files from /root/.my.cnf on node puppet.vagrant

does anybody know another solution? maybe something with exec, unless and test -f ?

cheers

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-07-28 16:27:33 -0500

timelord gravatar image

Create a custom facter in your module, for example:

require 'facter'

# Default for non-Linux nodes
#
Facter.add(:root_mycnf_exists) do
    setcode do
        nil
    end
end

# Linux
#
Facter.add(:root_mycnf_exists) do
    confine :kernel  => :linux
    setcode do
        FileTest.exists?("/root/.my.cnf")
    end
end

Then modify your code to look like this:

if $::root_mycnf_exists != nil and $::root_mycnf_exists {
   $mycnf = file ("/root/.my.cnf")
   if 'password' in $mycnf {
        #inlcude the mysql::server class without setting a root password
   }
}

I am pretty sure my code above can be optimized.

edit flag offensive delete link more

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-07-23 10:06:49 -0500

Seen: 668 times

Last updated: Jul 28 '14