Puppet : Automate/Reset first time root password for MySQL 5.7.x

Hello,

This article will help you reset first time root password for MySQL 5.7.x with Puppet.

I was looking for some custom puppet code/module to automate first root password for MySQL 5.7.x, I didn’t get proper puppet code to get through so I have decided to try and run my own code with puppet.

Puppet with MySQL 5.7
Puppet Labs

Pre-requisites :-

1. MySQL 5.7.x version should be installed before running this manifest/module.

Following are the steps to create Puppet module to reset first time password for MySQL 5.7.x

1. Make Puppet Module directory mysql

mkdir /etc/puppetlabs/code/environments/production/modules/

2. Create manifests directory.

mkdir /etc/puppetlabs/code/environments/production/modules/mysqlserver/manifests

3. Now create init.pp and put following code to the file.

class mysql57pwreset(
 $mysql_password = 'MySQL@99#') #Set password of your choice within single quotes
{
 
service { "mysqld":
 enable => true,
 ensure => running,
 }

$mysqlpwstring = "MYPW=$(grep 'temporary password' /var/log/mysqld.log | awk '{print \$NF}' | tail -n1) && mysql -uroot -p\${MYPW} --connect-expired-password -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '$mysql_password' PASSWORD EXPIRE NEVER;\""

File { "/tmp/mysql-pw.sh":
 content => $mysqlpwstring,
 mode => "0700",
 owner => "root",
 group => "root",
}


exec { "set-mysql-pw":
 path => [ '/bin', '/usr/bin', '/usr/local/bin', '/usr/sbin' ],
 unless => "mysqladmin -uroot -p$mysql_password status",
 require => Service["mysqld"],
 command => "sh /tmp/mysql-pw.sh",
 logoutput => on_failure,
}

}

4. Include mysql57pwreset module to /etc/puppetlabs/code/environments/production/manifests/site.pp

5. That’s it. Now you can execute puppet agent -t on puppet agent node, It will reset MySQL 5.7.x password.

Like us on Facebook : https://www.facebook.com/AllLinuxUsersBlog

DevOps Tools | AWS | Ansible | Chef | Puppet | Jenkins | GIT | SVN | OpenStack

Hello All,

I have written article and posts on Linux Fundamentals as well as Many articles on different different Linux Tools, Linux Services, Linux flavors. Time has now come to move to Advance tools, This is an era of Infrastructure automation, DevOps.

Logos

I will continue to write on Linux, Now Next Target is to write on DevOps tools like
Jenkins – Continuous Integration (CI) and Continuous Delivery (CD)
Ansible, Chef, Puppet – Infrastructure Automation
GIT, SVN – Source Code Management
Docker – Automates the deployment of applications inside software containers
OpenStack – Cloud Software
Amazon Web Services – Public Cloud

Subscribe yourself for Blog updates here: http://eepurl.com/basgDz

I would request you to keep visiting blog for this upcoming posts, Hope this will help you in future.

Note: All logos, trademarks and registered trademarks are the property of their respective owners.