Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_ad.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_ad
20 version_added: '1.9.0'
21 short_description: Manage FlashArray Active Directory Account
22 description:
23 - Add or delete FlashArray Active Directory Account
24 - FlashArray allows the creation of one AD computer account, or joining of an
25 existing AD computer account.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 name:
30 description:
31 - Name of the AD account
32 type: str
33 required: true
34 state:
35 description:
36 - Define whether the AD sccount is deleted or not
37 default: present
38 choices: [ absent, present ]
39 type: str
40 computer:
41 description:
42 - The common name of the computer account to be created in the Active Directory domain.
43 - If not specified, defaults to the name of the Active Directory configuration.
44 type: str
45 domain:
46 description:
47 - The Active Directory domain to join
48 type: str
49 username:
50 description:
51 - A user capable of creating a computer account within the domain
52 type: str
53 password:
54 description:
55 - Password string for I(username)
56 type: str
57 directory_servers:
58 description:
59 - A list of directory servers that will be used for lookups related to user authorization
60 - Accepted server formats are IP address and DNS name
61 - All specified servers must be registered to the domain appropriately in the array
62 configured DNS and are only communicated with over the secure LDAP (LDAPS) protocol.
63 If not specified, servers are resolved for the domain in DNS
64 - The specified list can have a maximum length of 1, or 3 for Purity 6.1.6 or higher.
65 If more are provided only the first allowed count used.
66 type: list
67 elements: str
68 kerberos_servers:
69 description:
70 - A list of key distribution servers to use for Kerberos protocol
71 - Accepted server formats are IP address and DNS name
72 - All specified servers must be registered to the domain appropriately in the array
73 configured DNS and are only communicated with over the secure LDAP (LDAPS) protocol.
74 If not specified, servers are resolved for the domain in DNS.
75 - The specified list can have a maximum length of 1, or 3 for Purity 6.1.6 or higher.
76 If more are provided only the first allowed count used.
77 type: list
78 elements: str
79 local_only:
80 description:
81 - Do a local-only delete of an active directory account
82 type: bool
83 default: false
84 join_ou:
85 description:
86 - Distinguished name of organization unit in which the computer account
87 should be created when joining the domain. e.g. OU=Arrays,OU=Storage.
88 - The B(DC=...) components can be omitted.
89 - If left empty, defaults to B(CN=Computers).
90 - Requires Purity//FA 6.1.8 or higher
91 type: str
92 version_added: '1.10.0'
93 tls:
94 description:
95 - TLS mode for communication with domain controllers.
96 type: str
97 choices: [ required, optional ]
98 default: required
99 version_added: '1.14.0'
100 join_existing:
101 description:
102 - If specified as I(true), the domain is searched for a pre-existing
103 computer account to join to, and no new account will be created within the domain.
104 The C(username) specified when joining a pre-existing account must have
105 permissions to 'read all properties from' and 'reset the password of'
106 the pre-existing account. C(join_ou) will be read from the pre-existing
107 account and cannot be specified when joining to an existing account
108 type: bool
109 default: false
110 version_added: '1.14.0'
111 extends_documentation_fragment:
112 - purestorage.flasharray.purestorage.fa
113 """
114
115 EXAMPLES = r"""
116 - name: Create new AD account
117 purestorage.flasharray.purefa_ad:
118 name: ad_account
119 computer: FLASHARRAY
120 domain: acme.com
121 join_ou: "OU=Acme,OU=Dev"
122 username: Administrator
123 password: Password
124 kerberos_servers:
125 - kdc.acme.com
126 directory_servers:
127 - ldap.acme.com
128 fa_url: 10.10.10.2
129 api_token: e31060a7-21fc-e277-6240-25983c6c4592
130
131 - name: Delete AD account locally
132 purestorage.flasharray.purefa_ad:
133 name: ad_account
134 local_only: true
135 fa_url: 10.10.10.2
136 api_token: e31060a7-21fc-e277-6240-25983c6c4592
137
138 - name: Fully delete AD account. Note that correct AD permissions are required
139 purestorage.flasharray.purefa_ad:
140 name: ad_account
141 fa_url: 10.10.10.2
142 api_token: e31060a7-21fc-e277-6240-25983c6c4592
143 """
144
145 RETURN = r"""
146 """
147
148 HAS_PURESTORAGE = True
149 try:
150 from pypureclient.flasharray import ActiveDirectoryPost, ActiveDirectoryPatch
151 except ImportError:
152 HAS_PURESTORAGE = False
153
154 from ansible.module_utils.basic import AnsibleModule
155 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
156 LooseVersion,
157 )
158 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
159 get_array,
160 purefa_argument_spec,
161 )
162
163 MIN_REQUIRED_API_VERSION = "2.2"
164 SERVER_API_VERSION = "2.6"
165 MIN_JOIN_OU_API_VERSION = "2.8"
166 MIN_TLS_API_VERSION = "2.15"
167
168
169 def delete_account(module, array):
170 """Delete Active directory Account"""
171 changed = True
172 if not module.check_mode:
173 res = array.delete_active_directory(
174 names=[module.params["name"]], local_only=module.params["local_only"]
175 )
176 if res.status_code != 200:
177 module.fail_json(
178 msg="Failed to delete AD Account {0}. Error: {1}".format(
179 module.params["name"], res.errors[0].message
180 )
181 )
182 module.exit_json(changed=changed)
183
184
185 def update_account(module, array):
186 """Update existing AD account"""
187 changed = False
188 current_acc = list(array.get_active_directory(names=[module.params["name"]]).items)[
189 0
190 ]
191 if current_acc.tls != module.params["tls"]:
192 changed = True
193 if not module.check_mode:
194 res = array.patch_active_directory(
195 names=[module.params["name"]],
196 active_directory=ActiveDirectoryPatch(tls=module.params["tls"]),
197 )
198 if res.status_code != 200:
199 module.fail_json(
200 msg="Failed to update AD Account {0} TLS setting. Error: {1}".format(
201 module.params["name"], res.errors[0].message
202 )
203 )
204 module.exit_json(changed=changed)
205
206
207 def create_account(module, array, api_version):
208 """Create Active Directory Account"""
209 changed = True
210 if LooseVersion(MIN_JOIN_OU_API_VERSION) > LooseVersion(api_version):
211 ad_config = ActiveDirectoryPost(
212 computer_name=module.params["computer"],
213 directory_servers=module.params["directory_servers"],
214 kerberos_servers=module.params["kerberos_servers"],
215 domain=module.params["domain"],
216 user=module.params["username"],
217 password=module.params["password"],
218 )
219 elif LooseVersion(MIN_TLS_API_VERSION) <= LooseVersion(api_version):
220 ad_config = ActiveDirectoryPost(
221 computer_name=module.params["computer"],
222 directory_servers=module.params["directory_servers"],
223 kerberos_servers=module.params["kerberos_servers"],
224 domain=module.params["domain"],
225 user=module.params["username"],
226 join_ou=module.params["join_ou"],
227 password=module.params["password"],
228 tls=module.params["tls"],
229 )
230 else:
231 ad_config = ActiveDirectoryPost(
232 computer_name=module.params["computer"],
233 directory_servers=module.params["directory_servers"],
234 kerberos_servers=module.params["kerberos_servers"],
235 domain=module.params["domain"],
236 user=module.params["username"],
237 join_ou=module.params["join_ou"],
238 password=module.params["password"],
239 )
240 if not module.check_mode:
241 if MIN_TLS_API_VERSION in api_version:
242 res = array.post_active_directory(
243 names=[module.params["name"]],
244 join_existing_account=module.params["join_existing"],
245 active_directory=ad_config,
246 )
247 else:
248 res = array.post_active_directory(
249 names=[module.params["name"]],
250 active_directory=ad_config,
251 )
252 if res.status_code != 200:
253 module.fail_json(
254 msg="Failed to add Active Directory Account {0}. Error: {1}".format(
255 module.params["name"], res.errors[0].message
256 )
257 )
258 module.exit_json(changed=changed)
259
260
261 def main():
262 argument_spec = purefa_argument_spec()
263 argument_spec.update(
264 dict(
265 state=dict(type="str", default="present", choices=["absent", "present"]),
266 username=dict(type="str"),
267 password=dict(type="str", no_log=True),
268 name=dict(type="str", required=True),
269 computer=dict(type="str"),
270 local_only=dict(type="bool", default=False),
271 domain=dict(type="str"),
272 join_ou=dict(type="str"),
273 directory_servers=dict(type="list", elements="str"),
274 kerberos_servers=dict(type="list", elements="str"),
275 tls=dict(type="str", default="required", choices=["required", "optional"]),
276 join_existing=dict(type="bool", default=False),
277 )
278 )
279
280 required_if = [["state", "present", ["username", "password", "domain"]]]
281
282 module = AnsibleModule(
283 argument_spec, required_if=required_if, supports_check_mode=True
284 )
285
286 if not HAS_PURESTORAGE:
287 module.fail_json(msg="py-pure-client sdk is required for this module")
288
289 array = get_array(module)
290 api_version = array.get_rest_version()
291 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
292 module.fail_json(
293 msg="FlashArray REST version not supported. "
294 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
295 )
296 state = module.params["state"]
297 exists = bool(
298 array.get_active_directory(names=[module.params["name"]]).status_code == 200
299 )
300
301 if not module.params["computer"]:
302 module.params["computer"] = module.params["name"].replace("_", "-")
303 if module.params["kerberos_servers"]:
304 if LooseVersion(SERVER_API_VERSION) <= LooseVersion(api_version):
305 module.params["kerberos_servers"] = module.params["kerberos_servers"][0:3]
306 else:
307 module.params["kerberos_servers"] = module.params["kerberos_servers"][0:1]
308 if module.params["directory_servers"]:
309 if LooseVersion(SERVER_API_VERSION) <= LooseVersion(api_version):
310 module.params["directory_servers"] = module.params["directory_servers"][0:3]
311 else:
312 module.params["directory_servers"] = module.params["directory_servers"][0:1]
313 if not exists and state == "present":
314 create_account(module, array, api_version)
315 elif (
316 exists
317 and state == "present"
318 and LooseVersion(MIN_TLS_API_VERSION) <= LooseVersion(api_version)
319 ):
320 update_account(module, array)
321 elif exists and state == "absent":
322 delete_account(module, array)
323
324 module.exit_json(changed=False)
325
326
327 if __name__ == "__main__":
328 main()