#!/usr/bin/python

import time
import json
import re
import os
import random
import sys

changed = False;

def generateid():
    changed = True
    return int(time.time()) + random.randint(0,1000)

def identify():
    try:
       config = file("/etc/my.cnf","r")
       for line in config: 
          match = re.match("^server-id = (\d+)\W+", line)
	  if match: 
	    mid = match.group(1)
    	    if mid == "1": return generateid()
            else: return mid
    except:
       return generateid() 

def main():
    module = AnsibleModule(
        argument_spec = dict(
            action 	  = dict(default='identify', choices=['identify']),
        )
    )

    action = module.params['action']
    
    if action == 'identify': 
	serverid = identify();
    	module.exit_json(changed=changed, server_id=serverid);

# ansible module snippets
from ansible.module_utils.basic import *
main()
