Created
August 20, 2015 19:18
-
-
Save jsumners/a74161a6a20838c5cb76 to your computer and use it in GitHub Desktop.
Ansible modules for PowerDNS's authoritative and recursive control utilities
This file contains 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/python | |
DOCUMENTATION = ''' | |
--- | |
module: pdns_control | |
author: "James Sumners" | |
short_description: Issue pdns_control commands | |
requirements: [ pdns_control ] | |
description: | |
- Issue PowerDNS authoritative server pdns_control commands | |
options: | |
command: | |
required: true | |
aliases: [ "cmnd" ] | |
description: | |
- The command to issue (e.g. "ccounts") | |
domain: | |
required: false | |
description: | |
- Required when issuing the "notify", "notify-host", or "retrieve" commands | |
host: | |
required: false | |
description: | |
- Required when issuing the "notify-host" command | |
record: | |
required: false | |
description: | |
- Used if present when issuing a "purge" command | |
variable: | |
required: false | |
aliases: [ "var" ] | |
description: | |
- Required when issuing the "set" or "show" commands | |
value: | |
required: false | |
aliases: [ "val" ] | |
description: | |
- Required when issuing the "set" command | |
''' | |
EXAMPLES = ''' | |
# Get cache statistics | |
- pdns_control: command=ccounts | |
# Notify a host to refresh domain example.com | |
- pnds_control: command="notify-host" domain=example.com host=some.slave | |
# Set a varible | |
- pdns_control: command=set var=foo val=bar | |
''' | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
command=dict(default=None, aliases=['cmnd'], type='str'), | |
domain=dict(default=None, type='str'), | |
host=dict(default=None, type='str'), | |
record=dict(default=None, type='str'), | |
variable=dict(default=None, aliases=['var'], type='str'), | |
value=dict(default=None, aliases=['val'], type='str') | |
), | |
supports_check_mode = False | |
) | |
cmd = module.params['command'].lower() | |
domain = module.params['domain'] | |
host = module.params['host'] | |
record = module.params['record'] | |
variable = module.params['variable'] | |
value = module.params['value'] | |
result = {} | |
rc = 0 | |
sout = '' | |
serr = '' | |
def runit(input): | |
params = [] | |
if type(input) is str: | |
params.append(input) | |
elif type(input) is list: | |
params = input | |
return module.run_command( | |
'pdns_control %s' % str.join(' ', params), check_rc=True | |
) | |
def todict(csv): | |
l = map(lambda s: s.strip(), csv.strip(',').split(',')) | |
d = {} | |
for ele in l: | |
k = '' | |
v = '' | |
if '=' in ele: | |
k, v = ele.split('=') | |
elif ':' in ele: | |
k, v = ele.split(':') | |
d[k.strip().replace(' ', '_')] = v.strip() | |
return d | |
if cmd == 'ccounts': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = todict(sout.strip()) | |
if cmd == 'cycle': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'list': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = todict(sout.strip()) | |
if cmd == 'notify': | |
if domain is None: | |
module.json_exit(msg='missing domain parameter') | |
else: | |
rc, sout, serr = runit([cmd, domain]) | |
result['msg'] = sout.strip() | |
if cmd == 'notify-host': | |
if domain is None: | |
module.json_exit(msg='missing domain parameter') | |
return | |
if host is None | |
module.json_exit(msg='missing host parameter') | |
return | |
rc, sout, serr = runit([cmd, domain, host]) | |
result['msg'] = sout.strip() | |
if cmd == 'ping': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'purge': | |
if record is not None: | |
rc, sout, serr = runit([cmd, record]) | |
else: | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'quit': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'rediscover': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'retrieve': | |
if domain is None: | |
module.json_exit(msg='missing domain parameter') | |
else: | |
rc, sout, serr = runit([cmd, domain]) | |
result['msg'] = sout.strip() | |
if cmd == 'show': | |
if variable is None: | |
module.json_exit(msg='missing variable parameter') | |
else: | |
rc, sout, serr = runit([cmd, variable]) | |
result['msg'] = sout.strip() | |
if cmd == 'status': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'uptime': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'version': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
module.exit_json(**result) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
main() |
This file contains 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/python | |
DOCUMENTATION = ''' | |
--- | |
module: rec_control | |
author: "James Sumners" | |
short_description: Issue rec_control commands | |
requirements: [ pdns_control ] | |
description: | |
- Issue PowerDNS recursive server rec_control commands | |
options: | |
command: | |
required: true | |
aliases: [ "cmnd" ] | |
description: | |
- The command to issue (e.g. "ping") | |
domain: | |
required: false | |
description: | |
- Required when issuing the "notify", "notify-host", or "retrieve" commands | |
filename: | |
required: false | |
aliases: [ "dest" ] | |
description: | |
- Required when issuing the "dump-cache" command | |
- This is a destination file on the remote system | |
- Use the fetch module to retrieve the file | |
statistic: | |
required: false | |
description: | |
- Required when issuing the "notify-host" command | |
''' | |
EXAMPLES = ''' | |
# Determine if the recursor is alove | |
- rec_control: command=ping | |
# Retrieve sys-msec statistic | |
- rec_control: command=get statistic="sys-msec" | |
# Wipe a domain from the cache | |
- rec_control: command="wipe-cache" domain=example.com | |
# Wipe multiple domains from the cache | |
- rec_control: command="wipe-cache" domain="www.example.com example.com" | |
''' | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
command=dict(default=None, aliases=['cmnd'], type='str'), | |
domain=dict(default=None, type='str'), | |
filename=dict(default=None, aliases=['dest'], type='str'), | |
statistic=dict(default=None, type='str') | |
), | |
supports_check_mode = False | |
) | |
cmd = module.params['command'].lower() | |
domain = module.params['domain'] | |
filename = module.params['filename'] | |
statistic = module.params['statistic'] | |
result = {} | |
rc = 0 | |
sout = '' | |
serr = '' | |
def runit(input): | |
params = [] | |
if type(input) is str: | |
params.append(input) | |
elif type(input) is list: | |
params = input | |
return module.run_command( | |
'rec_control %s' % str.join(' ', params), check_rc=True | |
) | |
def lines2dict(lines): | |
l = map(lambda s: s.strip(), lines.strip().split("\n")) | |
d = {} | |
for ele in l: | |
k, v = ele.split("\t") | |
d[k.strip().replace(' ', '_')] = v.strip() | |
return d | |
if cmd == 'dump-cache': | |
if filename is None: | |
module.json_exit(msg='missing filename parameter') | |
else: | |
rc, sout, serr = runit([cmd, filename]) | |
result['msg'] = sout.strip() | |
if cmd == 'get': | |
if statistic is None: | |
module.json_exit(msg='missing statistic parameter') | |
else: | |
rc, sout, serr = runit([cmd, statistic]) | |
result['msg'] = sout.strip() | |
if cmd == 'get-all': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = lines2dict(sout.strip()) | |
if cmd == 'ping': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'quit': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'reload-zones': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'top-remotes': | |
rc, sout, serr = runit(cmd) | |
result['msg'] = sout.strip() | |
if cmd == 'wipe-cache': | |
if domain is None: | |
module.json_exit(msg='missing domain parameter') | |
else: | |
rc, sout, serr = runit([cmd, domain]) | |
result['msg'] = sout.strip() | |
result['changed'] = (cmd == 'dump-cache') | |
module.exit_json(**result) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment