Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
12979cf8
Commit
12979cf8
authored
Jul 25, 2012
by
Mark Theunissen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change modules to use credentials in my.cnf if they are available
parent
fcd02e45
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
7 deletions
+60
-7
library/mysql_db
+31
-4
library/mysql_user
+29
-3
No files found.
library/mysql_db
View file @
12979cf8
...
...
@@ -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
:
...
...
@@ -43,6 +44,16 @@ def db_create(cursor, db):
res
=
cursor
.
execute
(
query
)
return
True
def
load_mycnf
():
config
=
ConfigParser
.
RawConfigParser
()
mycnf
=
os
.
path
.
expanduser
(
'~/.my.cnf'
)
config
.
read
(
mycnf
)
try
:
creds
=
dict
(
user
=
config
.
get
(
'client'
,
'user'
),
passwd
=
config
.
get
(
'client'
,
'pass'
))
except
ConfigParser
.
NoOptionError
:
return
False
return
creds
# ===========================================
# Module execution.
#
...
...
@@ -50,8 +61,8 @@ def db_create(cursor, db):
def
main
():
module
=
AnsibleModule
(
argument_spec
=
dict
(
loginuser
=
dict
(
default
=
"root"
),
loginpass
=
dict
(
default
=
""
),
loginuser
=
dict
(
default
=
None
),
loginpass
=
dict
(
default
=
None
),
loginhost
=
dict
(
default
=
"localhost"
),
db
=
dict
(
required
=
True
),
state
=
dict
(
default
=
"present"
,
choices
=
[
"absent"
,
"present"
]),
...
...
@@ -63,13 +74,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.
loginpass
=
module
.
params
[
"loginpass"
]
loginuser
=
module
.
params
[
"loginuser"
]
if
loginuser
is
None
and
loginpass
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"
]
loginpass
=
mycnf_creds
[
"passwd"
]
elif
loginpass
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
=
loginpass
,
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
)
...
...
library/mysql_user
View file @
12979cf8
...
...
@@ -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
:
...
...
@@ -141,6 +142,16 @@ 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'
)
config
.
read
(
mycnf
)
try
:
creds
=
dict
(
user
=
config
.
get
(
'client'
,
'user'
),
passwd
=
config
.
get
(
'client'
,
'pass'
))
except
ConfigParser
.
NoOptionError
:
return
False
return
creds
# ===========================================
# Module execution.
#
...
...
@@ -148,8 +159,8 @@ def privileges_grant(cursor, user,host,db_table,priv):
def
main
():
module
=
AnsibleModule
(
argument_spec
=
dict
(
loginuser
=
dict
(
default
=
"root"
),
loginpass
=
dict
(
default
=
""
),
loginuser
=
dict
(
default
=
None
),
loginpass
=
dict
(
default
=
None
),
loginhost
=
dict
(
default
=
"localhost"
),
user
=
dict
(
required
=
True
),
passwd
=
dict
(
default
=
None
),
...
...
@@ -173,8 +184,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.
loginpass
=
module
.
params
[
"loginpass"
]
loginuser
=
module
.
params
[
"loginuser"
]
if
loginuser
is
None
and
loginpass
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"
]
loginpass
=
mycnf_creds
[
"passwd"
]
elif
loginpass
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
=
loginpass
,
db
=
"mysql"
)
cursor
=
db_connection
.
cursor
()
except
Exception
as
e
:
module
.
fail_json
(
msg
=
"unable to connect to database"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment