Skip to content

Commit d1181d2

Browse files
authored
Add FreeIPA lookup plugins (#100)
Signed-off-by: rsuplina <rsuplina@cloudera.com>
1 parent efc4e78 commit d1181d2

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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_domain
20+
author: Ronald Suplina (@rsuplina) <rsuplina@cloudera.com>
21+
short_description: Get information about the FreeIPA domain and DNS server IP address(es) for the selected CDP Public Cloud Environment
22+
description:
23+
- Allows you to retrieve information about FreeIPA Domain for a given CDP Public Cloud Environment.
24+
- You can use these details to update client DNS, e.g. set up entries in /etc/resolv.conf
25+
- If the Environment is not found or is ambigious, the lookup will return an error.
26+
options:
27+
_terms:
28+
description:
29+
- A CDP Public Cloud Environment name
30+
type: string
31+
required: True
32+
detailed:
33+
description:
34+
- Flag to return the IP address of each FreeIP host for a selected CDP Public Cloud Environment.
35+
required: False
36+
type: boolean
37+
default: False
38+
39+
notes:
40+
- Requires C(cdpy).
41+
'''
42+
43+
44+
EXAMPLES = '''
45+
- name: Retrieve the FreeIPA domain and host IP addresses for a CDP Public Cloud Environment
46+
ansible.builtin.debug:
47+
msg: "{{ lookup('cloudera.cloud.env_freeipa_domain', 'example-env') }}"
48+
49+
- name: Retrieve the FreeIPA domain and host IP addresses for a CDP Public Cloud Environment
50+
ansible.builtin.debug:
51+
msg: "{{ lookup('cloudera.cloud.env_freeipa_domain', 'example-env' , detailed=True ) }}"
52+
53+
54+
'''
55+
56+
RETURN = '''
57+
_list:
58+
description: List of FreeIPA domains for selected Environments
59+
type: list
60+
elements: complex
61+
'''
62+
63+
from ansible.errors import AnsibleError
64+
from ansible.plugins.lookup import LookupBase
65+
from ansible.module_utils.common.text.converters import to_text, to_native
66+
from ansible.utils.display import Display
67+
68+
from cdpy.cdpy import Cdpy
69+
from cdpy.common import CdpError
70+
71+
72+
display = Display()
73+
74+
class LookupModule(LookupBase):
75+
def run(self, terms, variables=None, **kwargs):
76+
self.set_options(var_options=variables, direct=kwargs)
77+
78+
try:
79+
results = []
80+
for term in LookupBase._flatten(terms):
81+
environment = Cdpy().environments.describe_environment(term)
82+
freeipa_client_domain = environment['freeipa']['domain']
83+
84+
if self.get_option('detailed'):
85+
server_ips = environment['freeipa']['serverIP']
86+
results = [{'domain': freeipa_client_domain,'server_ips' : server_ips}]
87+
else:
88+
results = [ freeipa_client_domain ]
89+
return results
90+
91+
except KeyError as e:
92+
raise AnsibleError("Error parsing result: %s" % to_native(e))
93+
except CdpError as e:
94+
raise AnsibleError("Error connecting to CDP: %s" % to_native(e))
95+
96+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)