|  | 
|  | 1 | +# Copyright 2023 Cloudera, Inc. | 
|  | 2 | +# | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +#      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | + | 
|  | 15 | +from __future__ import (absolute_import, division, print_function) | 
|  | 16 | +__metaclass__ = type | 
|  | 17 | + | 
|  | 18 | +DOCUMENTATION = ''' | 
|  | 19 | +    lookup: env_freeipa_hosts | 
|  | 20 | +    author: Ronald Suplina (@rsuplina) <rsuplina@cloudera.com> | 
|  | 21 | +    short_description: Get information about FreeIPA hosts for selected Environment | 
|  | 22 | +    description: | 
|  | 23 | +        - Allows you to retrieve information about FreeIPA hosts for a given CDP Public Cloud Environment. | 
|  | 24 | +        - If the Environment is not found or is ambigious, the lookup will return an error. | 
|  | 25 | +    options: | 
|  | 26 | +        _terms: | 
|  | 27 | +            description: | 
|  | 28 | +                - A CDP Public Cloud Environment name | 
|  | 29 | +            type: string | 
|  | 30 | +            required: True | 
|  | 31 | +
 | 
|  | 32 | +        detailed: | 
|  | 33 | +            description: | 
|  | 34 | +                - Whether to return the full information about FreeIPA hosts for matching Environment  | 
|  | 35 | +            required: False | 
|  | 36 | +            type: boolean | 
|  | 37 | +            default: False | 
|  | 38 | +
 | 
|  | 39 | +    notes: | 
|  | 40 | +        - Requires C(cdpy). | 
|  | 41 | +''' | 
|  | 42 | + | 
|  | 43 | +EXAMPLES = ''' | 
|  | 44 | +- name: Retrieve the details for the FreeIPA hosts for a single CDP Public Cloud Environment | 
|  | 45 | +  ansible.builtin.debug: | 
|  | 46 | +    msg: "{{ lookup('cloudera.cloud.env_freeipa_hosts', environment='example-env-aws') }}" | 
|  | 47 | +
 | 
|  | 48 | +- name: Retrieve the details as list for the FreeIPA hosts for a single CDP Public Cloud Environment | 
|  | 49 | +  ansible.builtin.debug: | 
|  | 50 | +    msg: "{{ lookup('cloudera.cloud.env_freeipa_hosts', environment='example-env-aws', wantlist=True) }}" | 
|  | 51 | +
 | 
|  | 52 | +- name: Retrieve more detailied information for the FreeIPA hosts for a single CDP Public Cloud Environment | 
|  | 53 | +  ansible.builtin.debug: | 
|  | 54 | +    msg: "{{ lookup('cloudera.cloud.env_freeipa_hosts', environment='example-env-aws', detailed=True) }}" | 
|  | 55 | +     | 
|  | 56 | +''' | 
|  | 57 | + | 
|  | 58 | +RETURN = ''' | 
|  | 59 | +  _list: | 
|  | 60 | +    description: List of FreeIPA hosts information of selected Environment | 
|  | 61 | +    type: dict | 
|  | 62 | +    elements: complex | 
|  | 63 | +''' | 
|  | 64 | + | 
|  | 65 | +from ansible.errors import AnsibleError | 
|  | 66 | +from ansible.plugins.lookup import LookupBase | 
|  | 67 | +from ansible.module_utils.common.text.converters import to_text, to_native | 
|  | 68 | +from ansible.utils.display import Display | 
|  | 69 | + | 
|  | 70 | +from cdpy.cdpy import Cdpy | 
|  | 71 | +from cdpy.common import CdpError | 
|  | 72 | + | 
|  | 73 | + | 
|  | 74 | +display = Display() | 
|  | 75 | + | 
|  | 76 | +class LookupModule(LookupBase): | 
|  | 77 | +    def run(self, terms, variables=None, **kwargs): | 
|  | 78 | +        self.set_options(var_options=variables, direct=kwargs)   | 
|  | 79 | +            | 
|  | 80 | +        try: | 
|  | 81 | +            results = [] | 
|  | 82 | +            for term in LookupBase._flatten(terms): | 
|  | 83 | +                free_ipa_info = Cdpy().sdk.call(svc='environments', func='get_freeipa_status', environmentName=term)      | 
|  | 84 | + | 
|  | 85 | +                if self.get_option('detailed'): | 
|  | 86 | +                    for instance_id, instance_data in free_ipa_info['instances'].items(): | 
|  | 87 | +                        instance_data['id'] = instance_id   | 
|  | 88 | +                        results.append(instance_data) | 
|  | 89 | +                else: | 
|  | 90 | +                    results = [instance['hostname'] for instance in free_ipa_info['instances'].values()] | 
|  | 91 | +                return results | 
|  | 92 | + | 
|  | 93 | +        except KeyError as e: | 
|  | 94 | +            raise AnsibleError("Error parsing result: %s" % to_native(e)) | 
|  | 95 | +        except CdpError as e: | 
|  | 96 | +            raise AnsibleError("Error connecting to CDP: %s" % to_native(e)) | 
|  | 97 | + | 
0 commit comments