summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/dns/plugins/plugin_utils
diff options
context:
space:
mode:
Diffstat (limited to 'ansible_collections/community/dns/plugins/plugin_utils')
-rw-r--r--ansible_collections/community/dns/plugins/plugin_utils/inventory/records.py15
-rw-r--r--ansible_collections/community/dns/plugins/plugin_utils/ips.py27
-rw-r--r--ansible_collections/community/dns/plugins/plugin_utils/resolver.py49
3 files changed, 88 insertions, 3 deletions
diff --git a/ansible_collections/community/dns/plugins/plugin_utils/inventory/records.py b/ansible_collections/community/dns/plugins/plugin_utils/inventory/records.py
index 461a92b35..30fa11bdc 100644
--- a/ansible_collections/community/dns/plugins/plugin_utils/inventory/records.py
+++ b/ansible_collections/community/dns/plugins/plugin_utils/inventory/records.py
@@ -15,6 +15,7 @@ from ansible.module_utils import six
from ansible.module_utils.common._collections_compat import Sequence
from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.utils.display import Display
+from ansible.utils.unsafe_proxy import wrap_var as make_unsafe
from ansible.template import Templar
from ansible_collections.community.dns.plugins.module_utils.provider import (
@@ -65,7 +66,15 @@ class RecordsInventoryModule(BaseInventoryPlugin):
def parse(self, inventory, loader, path, cache=False):
super(RecordsInventoryModule, self).parse(inventory, loader, path, cache)
- self._read_config_data(path)
+ orig_config = self._read_config_data(path)
+
+ if 'filters' in orig_config:
+ display.deprecated(
+ 'The `filters` option of the %s inventory plugin has been renamed to `simple_filters`. '
+ 'The old name will stop working in community.dns 3.0.0.' % self.NAME,
+ collection_name='community.dns',
+ version='3.0.0',
+ )
self.templar = Templar(loader=loader)
@@ -109,7 +118,7 @@ class RecordsInventoryModule(BaseInventoryPlugin):
except DNSAPIError as e:
raise AnsibleError('Error: %s' % e)
- filters = self.get_option('filters')
+ filters = self.get_option('simple_filters')
filter_types = filters.get('type') or ['A', 'AAAA', 'CNAME']
if not isinstance(filter_types, Sequence) or isinstance(filter_types, six.string_types):
@@ -121,4 +130,4 @@ class RecordsInventoryModule(BaseInventoryPlugin):
if record.prefix:
name = '%s.%s' % (record.prefix, name)
self.inventory.add_host(name)
- self.inventory.set_variable(name, 'ansible_host', record.target)
+ self.inventory.set_variable(name, 'ansible_host', make_unsafe(record.target))
diff --git a/ansible_collections/community/dns/plugins/plugin_utils/ips.py b/ansible_collections/community/dns/plugins/plugin_utils/ips.py
new file mode 100644
index 000000000..256078794
--- /dev/null
+++ b/ansible_collections/community/dns/plugins/plugin_utils/ips.py
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2023, Felix Fontein <felix@fontein.de>
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+from __future__ import absolute_import, division, print_function
+__metaclass__ = type
+
+from ansible.errors import AnsibleError
+from ansible.module_utils.basic import missing_required_lib
+from ansible.module_utils.six import raise_from
+
+try:
+ import ipaddress # pylint: disable=unused-import
+except ImportError as exc:
+ IPADDRESS_IMPORT_EXC = exc
+else:
+ IPADDRESS_IMPORT_EXC = None
+
+
+def assert_requirements_present(plugin_name, plugin_type):
+ if IPADDRESS_IMPORT_EXC is not None:
+ msg = 'The {fqcn} {type} plugin is missing requirements: {msg}'.format(
+ msg=missing_required_lib('ipaddress'), fqcn=plugin_name, type=plugin_type
+ )
+ raise_from(AnsibleError(msg), IPADDRESS_IMPORT_EXC)
diff --git a/ansible_collections/community/dns/plugins/plugin_utils/resolver.py b/ansible_collections/community/dns/plugins/plugin_utils/resolver.py
new file mode 100644
index 000000000..99c875643
--- /dev/null
+++ b/ansible_collections/community/dns/plugins/plugin_utils/resolver.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2023, Felix Fontein <felix@fontein.de>
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+from __future__ import absolute_import, division, print_function
+__metaclass__ = type
+
+from ansible.errors import AnsibleError
+from ansible.module_utils.basic import missing_required_lib
+from ansible.module_utils.common.text.converters import to_native
+from ansible.module_utils.six import raise_from
+
+from ansible_collections.community.dns.plugins.module_utils.resolver import (
+ ResolverError,
+)
+
+try:
+ import dns # pylint: disable=unused-import
+ import dns.exception # pylint: disable=unused-import
+ import dns.name # pylint: disable=unused-import
+ import dns.message # pylint: disable=unused-import
+ import dns.query # pylint: disable=unused-import
+ import dns.rcode # pylint: disable=unused-import
+ import dns.rdatatype # pylint: disable=unused-import
+ import dns.resolver # pylint: disable=unused-import
+except ImportError as exc:
+ DNSPYTHON_IMPORTERROR = exc
+else:
+ DNSPYTHON_IMPORTERROR = None
+
+
+def guarded_run(runner, error_class=AnsibleError, server=None):
+ suffix = ' for {0}'.format(server) if server is not None else ''
+ try:
+ return runner()
+ except ResolverError as e:
+ raise_from(error_class('Unexpected resolving error{0}: {1}'.format(suffix, to_native(e))), e)
+ except dns.exception.DNSException as e:
+ raise_from(error_class('Unexpected DNS error{0}: {1}'.format(suffix, to_native(e))), e)
+
+
+def assert_requirements_present(plugin_name, plugin_type):
+ if DNSPYTHON_IMPORTERROR is not None:
+ msg = 'The {fqcn} {type} plugin is missing requirements: {msg}'.format(
+ msg=missing_required_lib('dnspython'), fqcn=plugin_name, type=plugin_type
+ )
+ raise_from(AnsibleError(msg), DNSPYTHON_IMPORTERROR)