Skip to content

Instantly share code, notes, and snippets.

Created September 2, 2016 10:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
an mysql lookup Module for Ansible
Example Usage:
{{ lookup('mysql', ('1x.1xx.x.12', 'username', 'passwd', 'db_name', 'table_name', 'key_name')) }}
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
import MySQLdb
except ImportError:
class LookupModule(LookupBase):
def run(self, terms, **kwargs):
if len(terms) != 6:
AnsibleError("The number of arguments are not right! ")
ret = []
host, user, passwd, db, table, key = (terms[0], terms[1], terms[2], terms[3], terms[4], terms[5])
conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db, port=3306)
cur = conn.cursor()
sql = 'select value from %s where keyname = "%s"' %(table, key)
result = cur.fetchone()
if result[0]:
return None
Copy link

Try *terms as argument instead of terms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment