Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import datetime
  5. import os
  6. from ansible.module_utils.basic import AnsibleModule
  7. from ansible.module_utils.mysql import mysql_driver, mysql_driver_fail_msg
  8.  
  9. def sql_anweisung(cursor, module, action, description):
  10. try:
  11. cursor.execute(action)
  12. except Exception as e:
  13. failmsg = 'Fehler beim ' + description + ': ' + e.args[1]
  14. module.fail_json(msg=failmsg)
  15. return True
  16.  
  17. def main():
  18. args = {'rootpw': {'required': True, 'type': 'str'}}
  19. module = AnsibleModule(argument_spec=args, supports_check_mode=True)
  20.  
  21. if mysql_driver is None:
  22. module.fail_json(msg=mysql_driver_fail_msg)
  23.  
  24. config_file = '/root/.my.cnf'
  25. try:
  26. if os.path.exists(config_file):
  27. mydb = mysql_driver.connect(read_default_file=config_file)
  28. else:
  29. mydb = mysql_driver.connect()
  30. except Exception:
  31. module.fail_json(msg='Verbindung zum Datenbank-Server nicht möglich!')
  32.  
  33. cursor = mydb.cursor()
  34.  
  35. if module.check_mode:
  36. module.exit_json(changed=True)
  37.  
  38. startd = datetime.datetime.now()
  39.  
  40. action = "UPDATE mysql.user SET Password=PASSWORD('%s') WHERE User='root';" % module.params['rootpw']
  41. sql_anweisung(cursor, module, action, 'Setzen des Root-Kennworts')
  42.  
  43. action = "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
  44. sql_anweisung(cursor, module, action, 'Ändern der Zugänge für Root')
  45.  
  46. action = "DELETE FROM mysql.user WHERE User='';"
  47. sql_anweisung(cursor, module, action, 'Löschen des anonymen Users')
  48.  
  49. action = 'DROP DATABASE IF EXISTS test;'
  50. sql_anweisung(cursor, module, action, 'Löschen der Test-Datenbank')
  51.  
  52. action = 'FLUSH PRIVILEGES;'
  53. changed = sql_anweisung(cursor, module, action, 'Modulabschluss')
  54.  
  55. endd = datetime.datetime.now()
  56. delta = endd - startd
  57.  
  58. result = {
  59. 'changed': changed,
  60. 'start': str(startd),
  61. 'end': str(endd),
  62. 'delta': str(delta),
  63. 'rootpw': module.params['rootpw'],
  64. 'message': 'Have a nice day!'
  65. }
  66.  
  67. module.exit_json(**result)
  68.  
  69. if __name__ == '__main__':
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement