Last active
September 14, 2018 11:05
-
-
Save VerosK/35461485f633a93a401271e2513f36ff to your computer and use it in GitHub Desktop.
ansible module example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from ansible.module_utils.basic import * | |
module = AnsibleModule( | |
argument_spec = dict( | |
hour = dict(default=12,type='int'), | |
), | |
supports_check_mode=True, | |
) | |
def main(): | |
from datetime import datetime | |
lunch_hour = module.params['hour'] | |
got_hour = datetime.now().hour | |
if got_hour == lunch_hour: | |
module.fail_json(msg="It's lunchtime") | |
else: | |
module.exit_json(changed=False, msg="It's OK", got=got_hour, expected=lunch_hour) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#import json | |
from ansible.module_utils.basic import * | |
module = AnsibleModule( | |
argument_spec = dict( | |
state = dict(default='present', choices=['present', 'absent']), | |
datafile = dict(required=True), | |
domain = dict(required=True), | |
master = dict(required=True), | |
type = dict(default='slave', choices=['master','slave']), | |
# something = dict(aliases=['whatever']) | |
), | |
supports_check_mode=False | |
) | |
def main(): | |
import sqlite3 | |
domain_name = module.params['domain'] | |
domain_master = module.params['master'] | |
domain_type = module.params['type'].upper() | |
database = sqlite3.connect(module.params['datafile']) | |
cursor = database.cursor() | |
cursor.execute('SELECT type,master FROM domains WHERE name=?', [domain_name]) | |
result = cursor.fetchall() | |
if len(result) == 0: | |
cursor.execute('INSERT into DOMAINS(name,master,type) VALUES (?,?,?)', | |
[domain_name, domain_master, domain_type]) | |
database.commit() | |
module.exit_json(changed=True, msg="Added domain %s" % domain_name) | |
return | |
if len(result) > 1: | |
module.fail_json(msg="Got more than two results for domains %s" % domain_name) | |
return | |
if result[0][0] == domain_type and result[0][1] == domain_master: | |
module.exit_json(changed=False) | |
return | |
cursor.execute('UPDATE DOMAINS SET master=?, type=? WHERE name=?', | |
[domain_master, domain_type, domain_name]) | |
database.commit() | |
module.exit_json(changed=True, msg="Updated domain %s" % domain_name) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment