mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
FreeIPA: Add support for TTL in dnsrecord (#41768)
Fixes: #33969 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
5abb63ea56
commit
065d9ed6ac
1 changed files with 60 additions and 21 deletions
|
@ -1,14 +1,17 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright: (c) 2017, Abhijeet Kasurde (akasurde@redhat.com)
|
# Copyright: (c) 2017, Abhijeet Kasurde (akasurde@redhat.com)
|
||||||
|
#
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {
|
||||||
'status': ['preview'],
|
'metadata_version': '1.1',
|
||||||
'supported_by': 'community'}
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
|
@ -46,6 +49,11 @@ options:
|
||||||
- In the case of 'PTR' record type, this will be the hostname.
|
- In the case of 'PTR' record type, this will be the hostname.
|
||||||
- In the case of 'TXT' record type, this will be a text.
|
- In the case of 'TXT' record type, this will be a text.
|
||||||
required: true
|
required: true
|
||||||
|
record_ttl:
|
||||||
|
description:
|
||||||
|
- Set the TTL for the record.
|
||||||
|
- Applies only when adding a new or changing the value of record_value.
|
||||||
|
version_added: "2.7"
|
||||||
state:
|
state:
|
||||||
description: State to ensure
|
description: State to ensure
|
||||||
required: false
|
required: false
|
||||||
|
@ -66,6 +74,17 @@ EXAMPLES = '''
|
||||||
record_type: 'AAAA'
|
record_type: 'AAAA'
|
||||||
record_value: '::1'
|
record_value: '::1'
|
||||||
|
|
||||||
|
# Ensure that dns record exists with a TTL
|
||||||
|
- ipa_dnsrecord:
|
||||||
|
name: host02
|
||||||
|
zone_name: example.com
|
||||||
|
record_type: 'AAAA'
|
||||||
|
record_value: '::1'
|
||||||
|
record_ttl: 300
|
||||||
|
ipa_host: ipa.example.com
|
||||||
|
ipa_pass: topsecret
|
||||||
|
state: present
|
||||||
|
|
||||||
# Ensure a PTR record is present
|
# Ensure a PTR record is present
|
||||||
- ipa_dnsrecord:
|
- ipa_dnsrecord:
|
||||||
ipa_host: spider.example.com
|
ipa_host: spider.example.com
|
||||||
|
@ -117,7 +136,7 @@ class DNSRecordIPAClient(IPAClient):
|
||||||
super(DNSRecordIPAClient, self).__init__(module, host, port, protocol)
|
super(DNSRecordIPAClient, self).__init__(module, host, port, protocol)
|
||||||
|
|
||||||
def dnsrecord_find(self, zone_name, record_name):
|
def dnsrecord_find(self, zone_name, record_name):
|
||||||
return self._post_json(method='dnsrecord_find', name=zone_name, item={'idnsname': record_name})
|
return self._post_json(method='dnsrecord_find', name=zone_name, item={'idnsname': record_name, 'all': True})
|
||||||
|
|
||||||
def dnsrecord_add(self, zone_name=None, record_name=None, details=None):
|
def dnsrecord_add(self, zone_name=None, record_name=None, details=None):
|
||||||
item = dict(idnsname=record_name)
|
item = dict(idnsname=record_name)
|
||||||
|
@ -136,11 +155,16 @@ class DNSRecordIPAClient(IPAClient):
|
||||||
elif details['record_type'] == 'TXT':
|
elif details['record_type'] == 'TXT':
|
||||||
item.update(txtrecord=details['record_value'])
|
item.update(txtrecord=details['record_value'])
|
||||||
|
|
||||||
|
if details.get('record_ttl'):
|
||||||
|
item.update(dnsttl=details['record_ttl'])
|
||||||
|
|
||||||
return self._post_json(method='dnsrecord_add', name=zone_name, item=item)
|
return self._post_json(method='dnsrecord_add', name=zone_name, item=item)
|
||||||
|
|
||||||
def dnsrecord_mod(self, zone_name=None, record_name=None, details=None):
|
def dnsrecord_mod(self, zone_name=None, record_name=None, details=None):
|
||||||
item = get_dnsrecord_dict(details)
|
item = get_dnsrecord_dict(details)
|
||||||
item.update(idnsname=record_name)
|
item.update(idnsname=record_name)
|
||||||
|
if details.get('record_ttl'):
|
||||||
|
item.update(dnsttl=details['record_ttl'])
|
||||||
return self._post_json(method='dnsrecord_mod', name=zone_name, item=item)
|
return self._post_json(method='dnsrecord_mod', name=zone_name, item=item)
|
||||||
|
|
||||||
def dnsrecord_del(self, zone_name=None, record_name=None, details=None):
|
def dnsrecord_del(self, zone_name=None, record_name=None, details=None):
|
||||||
|
@ -165,6 +189,10 @@ def get_dnsrecord_dict(details=None):
|
||||||
module_dnsrecord.update(ptrrecord=details['record_value'])
|
module_dnsrecord.update(ptrrecord=details['record_value'])
|
||||||
elif details['record_type'] == 'TXT' and details['record_value']:
|
elif details['record_type'] == 'TXT' and details['record_value']:
|
||||||
module_dnsrecord.update(txtrecord=details['record_value'])
|
module_dnsrecord.update(txtrecord=details['record_value'])
|
||||||
|
|
||||||
|
if details.get('record_ttl'):
|
||||||
|
module_dnsrecord.update(dnsttl=details['record_ttl'])
|
||||||
|
|
||||||
return module_dnsrecord
|
return module_dnsrecord
|
||||||
|
|
||||||
|
|
||||||
|
@ -176,11 +204,15 @@ def get_dnsrecord_diff(client, ipa_dnsrecord, module_dnsrecord):
|
||||||
def ensure(module, client):
|
def ensure(module, client):
|
||||||
zone_name = module.params['zone_name']
|
zone_name = module.params['zone_name']
|
||||||
record_name = module.params['record_name']
|
record_name = module.params['record_name']
|
||||||
|
record_ttl = module.params.get('record_ttl')
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
ipa_dnsrecord = client.dnsrecord_find(zone_name, record_name)
|
ipa_dnsrecord = client.dnsrecord_find(zone_name, record_name)
|
||||||
module_dnsrecord = dict(record_type=module.params['record_type'],
|
module_dnsrecord = dict(
|
||||||
record_value=module.params['record_value'])
|
record_type=module.params['record_type'],
|
||||||
|
record_value=module.params['record_value'],
|
||||||
|
record_ttl=to_native(record_ttl),
|
||||||
|
)
|
||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
|
@ -212,25 +244,32 @@ def ensure(module, client):
|
||||||
def main():
|
def main():
|
||||||
record_types = ['A', 'AAAA', 'A6', 'CNAME', 'DNAME', 'PTR', 'TXT']
|
record_types = ['A', 'AAAA', 'A6', 'CNAME', 'DNAME', 'PTR', 'TXT']
|
||||||
argument_spec = ipa_argument_spec()
|
argument_spec = ipa_argument_spec()
|
||||||
argument_spec.update(zone_name=dict(type='str', required=True),
|
argument_spec.update(
|
||||||
record_name=dict(type='str', aliases=['name'], required=True),
|
zone_name=dict(type='str', required=True),
|
||||||
record_type=dict(type='str', default='A', choices=record_types),
|
record_name=dict(type='str', aliases=['name'], required=True),
|
||||||
record_value=dict(type='str', required=True),
|
record_type=dict(type='str', default='A', choices=record_types),
|
||||||
state=dict(type='str', default='present', choices=['present', 'absent']),
|
record_value=dict(type='str', required=True),
|
||||||
)
|
state=dict(type='str', default='present', choices=['present', 'absent']),
|
||||||
|
record_ttl=dict(type='int'),
|
||||||
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(
|
||||||
supports_check_mode=True
|
argument_spec=argument_spec,
|
||||||
)
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
client = DNSRecordIPAClient(module=module,
|
client = DNSRecordIPAClient(
|
||||||
host=module.params['ipa_host'],
|
module=module,
|
||||||
port=module.params['ipa_port'],
|
host=module.params['ipa_host'],
|
||||||
protocol=module.params['ipa_prot'])
|
port=module.params['ipa_port'],
|
||||||
|
protocol=module.params['ipa_prot']
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client.login(username=module.params['ipa_user'],
|
client.login(
|
||||||
password=module.params['ipa_pass'])
|
username=module.params['ipa_user'],
|
||||||
|
password=module.params['ipa_pass']
|
||||||
|
)
|
||||||
changed, record = ensure(module, client)
|
changed, record = ensure(module, client)
|
||||||
module.exit_json(changed=changed, record=record)
|
module.exit_json(changed=changed, record=record)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Add table
Reference in a new issue