Roopendra
Roopendra

Reputation: 7776

Unable to import MySQLdb in ansible module

I am trying to write custom module in ansible. while using import MySQLdb it is giving me error

 failed: [127.0.0.1] => {"failed": true, "parsed": false}
invalid output was: Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-1394199347.29-33439012674717/inventory", line 11, in <module>
    import MySQLdb
ImportError: No module named MySQLdb

Using
Python Version : 2.6.6
MySQL-python Version : 1.2.3

Python Code:-

#!/usr/bin/python

import datetime
import sys
import json
import os
import shlex
import MySQLdb

db = MySQLdb.connect("localhost","user","pwd","db_name" )
cursor = db.cursor()
cursor.execute("SELECT * FROM hosts")
data = cursor.fetchone()
print data
db.close()

I have written a playbook to run ansible module:-

inventory.yaml:-

---
- hosts: webservers
  user: root
  sudo: True
  vars:
     act: list
  tasks:
  - name: Run module inventory
    action: inventory act="{{act}}" prod="roop"

I'm running this playbook using below commands:-

ansible-playbook -v playboook/path/inventory.yaml 

Same code working in python command line (<<<) but not working in ansible module.

In my ansible module other code are working. Is there any configuration setting need to do for ansible??

Upvotes: 2

Views: 3463

Answers (1)

Rico
Rico

Reputation: 61689

Your MySQLdb module is not in the Python environment. In your script you are running python using: #!/usr/bin/python. It's quite possible that the Python that you are running from the command line (>>>) is not the same Python. You can try running:

which python

to find out which Python you are running off of. So, if it's another Python to install it, you can run on Ubuntu:

sudo apt-get install python-mysqldb

or on CentOS, RedHat:

sudo yum install MySQL-python

Hope it helps.

Upvotes: 6

Related Questions