Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change modules to use credentials in my.cnf if they are available #690

Merged
merged 5 commits into from
Jul 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions library/mysql_db
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

import ConfigParser
try:
import MySQLdb
except ImportError:
Expand All @@ -43,15 +44,27 @@ def db_create(cursor, db):
res = cursor.execute(query)
return True

def load_mycnf():
config = ConfigParser.RawConfigParser()
mycnf = os.path.expanduser('~/.my.cnf')
if not os.path.exists(mycnf):
return False
try:
config.readfp(open(mycnf))
creds = dict(user=config.get('client', 'user'),passwd=config.get('client', 'pass'))
except (ConfigParser.NoOptionError, IOError):
return False
return creds

# ===========================================
# Module execution.
#

def main():
module = AnsibleModule(
argument_spec = dict(
loginuser=dict(default="root"),
loginpass=dict(default=""),
loginuser=dict(default=None),
loginpasswd=dict(default=None),
loginhost=dict(default="localhost"),
db=dict(required=True),
state=dict(default="present", choices=["absent", "present"]),
Expand All @@ -63,13 +76,29 @@ def main():

db = module.params["db"]
state = module.params["state"]
changed = False

# Either the caller passes both a username and password with which to connect to
# mysql, or they pass neither and allow this module to read the credentials from
# ~/.my.cnf.
loginpasswd = module.params["loginpasswd"]
loginuser = module.params["loginuser"]
if loginuser is None and loginpasswd is None:
mycnf_creds = load_mycnf()
if mycnf_creds is False:
module.fail_json(msg="incomplete login arguments passed and can't find them in ~/.my.cnf")
else:
loginuser = mycnf_creds["user"]
loginpasswd = mycnf_creds["passwd"]
elif loginpasswd is None or loginuser is None:
module.fail_json(msg="when supplying login arguments, both user and pass must be provided")

try:
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=module.params["loginuser"], passwd=module.params["loginpass"], db="mysql")
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=loginuser, passwd=loginpasswd, db="mysql")
cursor = db_connection.cursor()
except Exception as e:
module.fail_json(msg="unable to connect to database")

changed = False
if db_exists(cursor, db):
if state == "absent":
changed = db_delete(cursor, db)
Expand Down
34 changes: 31 additions & 3 deletions library/mysql_user
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

import ConfigParser
try:
import MySQLdb
except ImportError:
Expand Down Expand Up @@ -141,15 +142,27 @@ def privileges_grant(cursor, user,host,db_table,priv):
query = "GRANT %s ON %s TO '%s'@'%s'" % (priv_string,db_table,user,host)
cursor.execute(query)

def load_mycnf():
config = ConfigParser.RawConfigParser()
mycnf = os.path.expanduser('~/.my.cnf')
if not os.path.exists(mycnf):
return False
try:
config.readfp(open(mycnf))
creds = dict(user=config.get('client', 'user'),passwd=config.get('client', 'pass'))
except (ConfigParser.NoOptionError, IOError):
return False
return creds

# ===========================================
# Module execution.
#

def main():
module = AnsibleModule(
argument_spec = dict(
loginuser=dict(default="root"),
loginpass=dict(default=""),
loginuser=dict(default=None),
loginpasswd=dict(default=None),
loginhost=dict(default="localhost"),
user=dict(required=True),
passwd=dict(default=None),
Expand All @@ -173,8 +186,23 @@ def main():
except:
module.fail_json(msg="invalid privileges string")

# Either the caller passes both a username and password with which to connect to
# mysql, or they pass neither and allow this module to read the credentials from
# ~/.my.cnf.
loginpasswd = module.params["loginpasswd"]
loginuser = module.params["loginuser"]
if loginuser is None and loginpasswd is None:
mycnf_creds = load_mycnf()
if mycnf_creds is False:
module.fail_json(msg="incomplete login arguments passed and can't find them in ~/.my.cnf")
else:
loginuser = mycnf_creds["user"]
loginpasswd = mycnf_creds["passwd"]
elif loginpasswd is None or loginuser is None:
module.fail_json(msg="when supplying login arguments, both user and pass must be provided")

try:
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=module.params["loginuser"], passwd=module.params["loginpass"], db="mysql")
db_connection = MySQLdb.connect(host=module.params["loginhost"], user=loginuser, passwd=loginpasswd, db="mysql")
cursor = db_connection.cursor()
except Exception as e:
module.fail_json(msg="unable to connect to database")
Expand Down