Support one to many A records in nsupdate module (#25620)

* Updating the nsupdate module to accept a list for 'value' instead
of a string. This is to allow manipulating 1:many DNS records.

A string can still be supplied so it should be backwards compatible.

Addresses issue #25554

* Update nsupdate.py

* Update nsupdate.py

* Update nsupdate.py
This commit is contained in:
David Critch 2017-09-13 18:21:48 -04:00 committed by ansibot
parent ac6205b9e8
commit e462e3bb3f

View file

@ -84,6 +84,15 @@ EXAMPLES = '''
record: "ansible" record: "ansible"
value: "192.168.1.1" value: "192.168.1.1"
- name: Add or modify ansible.example.org A to 192.168.1.1, 192.168.1.2 and 192.168.1.3"
nsupdate:
key_name: "nsupdate"
key_secret: "+bFQtBCta7j2vWkjPkAFtgA=="
server: "10.1.1.1"
zone: "example.org"
record: "ansible"
value: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
- name: Remove puppet.example.org CNAME - name: Remove puppet.example.org CNAME
nsupdate: nsupdate:
key_name: "nsupdate" key_name: "nsupdate"
@ -116,9 +125,9 @@ type:
type: string type: string
sample: 'CNAME' sample: 'CNAME'
value: value:
description: DNS record value description: DNS record value(s)
returned: success returned: success
type: string type: list
sample: '192.168.1.1' sample: '192.168.1.1'
zone: zone:
description: DNS record zone description: DNS record zone
@ -223,11 +232,12 @@ class RecordManager(object):
def create_record(self): def create_record(self):
update = dns.update.Update(self.zone, keyring=self.keyring, keyalgorithm=self.algorithm) update = dns.update.Update(self.zone, keyring=self.keyring, keyalgorithm=self.algorithm)
for entry in self.module.params['value']:
try: try:
update.add(self.module.params['record'], update.add(self.module.params['record'],
self.module.params['ttl'], self.module.params['ttl'],
self.module.params['type'], self.module.params['type'],
self.module.params['value']) entry)
except AttributeError: except AttributeError:
self.module.fail_json(msg='value needed when state=present') self.module.fail_json(msg='value needed when state=present')
except dns.exception.SyntaxError: except dns.exception.SyntaxError:
@ -238,12 +248,19 @@ class RecordManager(object):
def modify_record(self): def modify_record(self):
update = dns.update.Update(self.zone, keyring=self.keyring, keyalgorithm=self.algorithm) update = dns.update.Update(self.zone, keyring=self.keyring, keyalgorithm=self.algorithm)
update.replace(self.module.params['record'], update.delete(self.module.params['record'], self.module.params['type'])
for entry in self.module.params['value']:
try:
update.add(self.module.params['record'],
self.module.params['ttl'], self.module.params['ttl'],
self.module.params['type'], self.module.params['type'],
self.module.params['value']) entry)
except AttributeError:
self.module.fail_json(msg='value needed when state=present')
except dns.exception.SyntaxError:
self.module.fail_json(msg='Invalid/malformed value')
response = self.__do_update(update) response = self.__do_update(update)
return dns.message.Message.rcode(response) return dns.message.Message.rcode(response)
def remove_record(self): def remove_record(self):
@ -282,8 +299,9 @@ class RecordManager(object):
if self.dns_rc == 0: if self.dns_rc == 0:
if self.module.params['state'] == 'absent': if self.module.params['state'] == 'absent':
return 1 return 1
for entry in self.module.params['value']:
try: try:
update.present(self.module.params['record'], self.module.params['type'], self.module.params['value']) update.present(self.module.params['record'], self.module.params['type'], entry)
except AttributeError: except AttributeError:
self.module.fail_json(msg='value needed when state=present') self.module.fail_json(msg='value needed when state=present')
except dns.exception.SyntaxError: except dns.exception.SyntaxError:
@ -313,7 +331,7 @@ def main():
record=dict(required=True, type='str'), record=dict(required=True, type='str'),
type=dict(required=False, default='A', type='str'), type=dict(required=False, default='A', type='str'),
ttl=dict(required=False, default=3600, type='int'), ttl=dict(required=False, default=3600, type='int'),
value=dict(required=False, default=None, type='str') value=dict(required=False, default=None, type='list')
), ),
supports_check_mode=True supports_check_mode=True
) )