diff --git a/lib/ansible/modules/net_tools/nsupdate.py b/lib/ansible/modules/net_tools/nsupdate.py index 8cda9911991..81dcd42d939 100644 --- a/lib/ansible/modules/net_tools/nsupdate.py +++ b/lib/ansible/modules/net_tools/nsupdate.py @@ -59,10 +59,11 @@ options: zone: description: - DNS record will be modified on this C(zone). - required: true + - When omitted DNS will be queried to attempt finding the correct zone. + - Starting with Ansible 2.7 this parameter is optional. record: description: - - Sets the DNS record to modify. + - Sets the DNS record to modify. When zone is omitted this has to be absolute (ending with a dot). required: true type: description: @@ -172,11 +173,23 @@ class RecordManager(object): def __init__(self, module): self.module = module - if module.params['zone'][-1] != '.': - self.zone = module.params['zone'] + '.' + if module.params['zone'] is None: + if module.params['record'][-1] != '.': + self.module.fail_json(msg='record must be absolute when omitting zone parameter') + + try: + self.zone = dns.resolver.zone_for_name(self.module.params['record']).to_text() + except (dns.exception.Timeout, dns.resolver.NoNameservers, dns.resolver.NoRootSOA) as e: + self.module.fail_json(msg='Zone resolver error (%s): %s' % (e.__class__.__name__, to_native(e))) + + if self.zone is None: + self.module.fail_json(msg='Unable to find zone, dnspython returned None') else: self.zone = module.params['zone'] + if self.zone[-1] != '.': + self.zone += '.' + if module.params['key_name']: try: self.keyring = dns.tsigkeyring.from_text({ @@ -332,7 +345,7 @@ def main(): key_name=dict(required=False, type='str'), key_secret=dict(required=False, type='str', no_log=True), key_algorithm=dict(required=False, default='hmac-md5', choices=tsig_algs, type='str'), - zone=dict(required=True, type='str'), + zone=dict(required=False, default=None, type='str'), record=dict(required=True, type='str'), type=dict(required=False, default='A', type='str'), ttl=dict(required=False, default=3600, type='int'),