| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: redhat_manifest |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Interact with a Red Hat Satellite Subscription Manifest |
| 27 |
description: |
| 28 |
- Download and modify a Red Hat Satellite Subscription Manifest |
| 29 |
author: |
| 30 |
- "Sean O'Keeffe (@sean797)" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Manifest Name |
| 35 |
type: str |
| 36 |
uuid: |
| 37 |
description: |
| 38 |
- Manifest uuid |
| 39 |
type: str |
| 40 |
username: |
| 41 |
description: |
| 42 |
- Red Hat Portal username |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
password: |
| 46 |
description: |
| 47 |
- Red Hat Portal password |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
pool_id: |
| 51 |
description: |
| 52 |
- Subscription pool_id |
| 53 |
type: str |
| 54 |
quantity: |
| 55 |
description: |
| 56 |
- quantity of pool_id Subscriptions |
| 57 |
type: int |
| 58 |
default: 1 |
| 59 |
pool_state: |
| 60 |
description: |
| 61 |
- Subscription state |
| 62 |
default: present |
| 63 |
choices: |
| 64 |
- present |
| 65 |
- absent |
| 66 |
type: str |
| 67 |
state: |
| 68 |
description: |
| 69 |
- Manifest state |
| 70 |
default: present |
| 71 |
choices: |
| 72 |
- present |
| 73 |
- absent |
| 74 |
type: str |
| 75 |
path: |
| 76 |
description: |
| 77 |
- path to export the manifest |
| 78 |
type: path |
| 79 |
validate_certs: |
| 80 |
description: |
| 81 |
- Validate Portal SSL |
| 82 |
default: true |
| 83 |
type: bool |
| 84 |
portal: |
| 85 |
description: |
| 86 |
- Red Hat Portal subscription access address |
| 87 |
default: https://subscription.rhsm.redhat.com |
| 88 |
type: str |
| 89 |
content_access_mode: |
| 90 |
description: |
| 91 |
- Content Access Mode of the Subscription Manifest. |
| 92 |
- Setting I(content_access_mode=org_enviroment) enables Simple Content Access. |
| 93 |
type: str |
| 94 |
choices: |
| 95 |
- org_environment |
| 96 |
- entitlement |
| 97 |
default: entitlement |
| 98 |
''' |
| 99 |
|
| 100 |
EXAMPLES = ''' |
| 101 |
- name: Create foreman.example.com Manifest and add 7 sub |
| 102 |
theforeman.foreman.redhat_manifest: |
| 103 |
name: "foreman.example.com" |
| 104 |
username: "john-smith" |
| 105 |
password: "changeme" |
| 106 |
pool_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 107 |
quantity: 7 |
| 108 |
|
| 109 |
- name: Ensure my manifest has 10 of one subs in it and export |
| 110 |
theforeman.foreman.redhat_manifest: |
| 111 |
uuid: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 112 |
username: john-smith |
| 113 |
password: changeme |
| 114 |
pool_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 115 |
quantity: 10 |
| 116 |
path: /root/manifest.zip |
| 117 |
|
| 118 |
- name: Remove all of one subs from foreman.example.com |
| 119 |
theforeman.foreman.redhat_manifest: |
| 120 |
name: foreman.example.com |
| 121 |
username: john-smith |
| 122 |
password: changeme |
| 123 |
pool_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 124 |
pool_state: absent |
| 125 |
''' |
| 126 |
|
| 127 |
RETURN = ''' |
| 128 |
uuid: |
| 129 |
description: Manifest UUID |
| 130 |
returned: success |
| 131 |
type: str |
| 132 |
sample: 5349d1d0-5bda-480a-b7bd-ff41e2c29e03 |
| 133 |
version_added: 3.8.0 |
| 134 |
''' |
| 135 |
|
| 136 |
import json |
| 137 |
import os |
| 138 |
import tempfile |
| 139 |
|
| 140 |
from ansible.module_utils.basic import AnsibleModule |
| 141 |
from ansible.module_utils.urls import fetch_url |
| 142 |
from ansible.module_utils.common.text.converters import to_text, to_native |
| 143 |
|
| 144 |
|
| 145 |
REDHAT_UEP = '/etc/rhsm/ca/redhat-uep.pem' |
| 146 |
|
| 147 |
|
| 148 |
def fetch_portal(module, path, method, data=None, accept_header='application/json'): |
| 149 |
headers = {'accept': accept_header} |
| 150 |
if data is not None: |
| 151 |
data = json.dumps(data) |
| 152 |
headers['content-type'] = 'application/json' |
| 153 |
url = module.params['portal'] + path |
| 154 |
fetch_kwargs = {'timeout': 30} |
| 155 |
if os.path.exists(REDHAT_UEP): |
| 156 |
fetch_kwargs['ca_path'] = REDHAT_UEP |
| 157 |
resp, info = fetch_url(module, url, data, headers, method, **fetch_kwargs) |
| 158 |
if resp is None or info["status"] >= 400: |
| 159 |
try: |
| 160 |
error = json.loads(info['body'])['displayMessage'] |
| 161 |
except Exception: |
| 162 |
error = info['msg'] |
| 163 |
module.fail_json(msg="%s to %s failed, got %s" % (method, url, error)) |
| 164 |
return resp, info |
| 165 |
|
| 166 |
|
| 167 |
def create_manifest(module): |
| 168 |
path = "/subscription/consumers" |
| 169 |
data = {'name': module.params['name'], |
| 170 |
'type': "satellite", |
| 171 |
'contentAccessMode': module.params['content_access_mode'], |
| 172 |
|
| 173 |
|
| 174 |
'facts': {'distributor_version': 'sat-6.3', |
| 175 |
'system.certificate_version': '3.2'}} |
| 176 |
resp, info = fetch_portal(module, path, 'POST', data) |
| 177 |
return json.loads(to_text(resp.read())) |
| 178 |
|
| 179 |
|
| 180 |
def delete_manifest(module, uuid): |
| 181 |
path = "/subscription/consumers/%s" % uuid |
| 182 |
resp, info = fetch_portal(module, path, 'DELETE') |
| 183 |
if info['status'] != 204: |
| 184 |
module.fail_json(msg="Got status %s attempting to delete manifest, expected 204" % (info['status'])) |
| 185 |
|
| 186 |
|
| 187 |
def get_manifest(module): |
| 188 |
path = "/subscription/owners/%s/consumers?type=satellite" % (module.params['rhsm_owner']) |
| 189 |
if module.params['uuid']: |
| 190 |
path += '&uuid={0}'.format(module.params['uuid']) |
| 191 |
resp, info = fetch_portal(module, path, 'GET') |
| 192 |
manifests = json.loads(to_text(resp.read())) |
| 193 |
if module.params['name']: |
| 194 |
attr = 'name' |
| 195 |
if module.params['uuid']: |
| 196 |
attr = 'uuid' |
| 197 |
manifest = [m for m in manifests if m[attr] == module.params[attr]] |
| 198 |
if manifest: |
| 199 |
if module.params['state'] == 'present': |
| 200 |
return manifest[0], False |
| 201 |
if module.params['state'] == 'absent': |
| 202 |
if not module.check_mode: |
| 203 |
return delete_manifest(module, manifest[0]['uuid']), True |
| 204 |
return None, True |
| 205 |
elif module.params['state'] == 'present': |
| 206 |
if not module.check_mode: |
| 207 |
return create_manifest(module), True |
| 208 |
return None, True |
| 209 |
return None, False |
| 210 |
|
| 211 |
|
| 212 |
def get_owner(module): |
| 213 |
path = "/subscription/users/%s/owners" % (module.params['username']) |
| 214 |
resp, info = fetch_portal(module, path, 'GET') |
| 215 |
return json.loads(to_text(resp.read()))[0]['key'] |
| 216 |
|
| 217 |
|
| 218 |
def get_subs(module, manifest): |
| 219 |
path = "/subscription/consumers/%s/entitlements" % (manifest['uuid']) |
| 220 |
resp, info = fetch_portal(module, path, 'GET') |
| 221 |
all_subs = json.loads(to_text(resp.read())) |
| 222 |
subs = [s for s in all_subs if s['pool']['id'] == module.params['pool_id']] |
| 223 |
return subs |
| 224 |
|
| 225 |
|
| 226 |
def get_remove_or_attach_sub(module, manifest): |
| 227 |
changed = False |
| 228 |
subs = get_subs(module, manifest) |
| 229 |
if subs: |
| 230 |
if module.params['pool_state'] == 'present': |
| 231 |
sub_quantity = sum(s['quantity'] for s in subs) |
| 232 |
while sub_quantity > module.params['quantity']: |
| 233 |
if not module.check_mode: |
| 234 |
remove_sub(module, manifest, subs[0]) |
| 235 |
else: |
| 236 |
changed = True |
| 237 |
break |
| 238 |
changed = True |
| 239 |
subs = get_subs(module, manifest) |
| 240 |
sub_quantity = sum(s['quantity'] for s in subs) |
| 241 |
if sub_quantity < module.params['quantity']: |
| 242 |
difference = module.params['quantity'] - sub_quantity |
| 243 |
if not module.check_mode: |
| 244 |
attach_sub(module, manifest, difference) |
| 245 |
changed = True |
| 246 |
elif module.params['pool_state'] == 'absent': |
| 247 |
if not module.check_mode: |
| 248 |
for sub in subs: |
| 249 |
remove_sub(module, manifest, sub) |
| 250 |
changed = True |
| 251 |
elif module.params['pool_state'] == 'present': |
| 252 |
if not module.check_mode: |
| 253 |
attach_sub(module, manifest, module.params['quantity']) |
| 254 |
changed = True |
| 255 |
return changed |
| 256 |
|
| 257 |
|
| 258 |
def remove_sub(module, manifest, sub): |
| 259 |
path = "/subscription/consumers/%s/entitlements/%s" % (manifest['uuid'], sub['id']) |
| 260 |
fetch_portal(module, path, 'DELETE') |
| 261 |
|
| 262 |
|
| 263 |
def attach_sub(module, manifest, quantity): |
| 264 |
path = "/subscription/consumers/%s/entitlements?pool=%s&quantity=%s" % (manifest['uuid'], module.params['pool_id'], quantity) |
| 265 |
fetch_portal(module, path, 'POST') |
| 266 |
|
| 267 |
|
| 268 |
def export_manifest(module, manifest): |
| 269 |
path = "/subscription/consumers/%s/export" % (manifest['uuid']) |
| 270 |
try: |
| 271 |
resp, info = fetch_portal(module, path, 'GET', accept_header='application/zip') |
| 272 |
fd, tempname = tempfile.mkstemp(dir=module.tmpdir) |
| 273 |
with os.fdopen(fd, 'wb') as f: |
| 274 |
while True: |
| 275 |
data = resp.read(65536) |
| 276 |
if not data: |
| 277 |
break |
| 278 |
f.write(data) |
| 279 |
|
| 280 |
checksum_src = module.sha1(tempname) |
| 281 |
checksum_dest = module.sha1(module.params['path']) |
| 282 |
|
| 283 |
changed = checksum_src != checksum_dest |
| 284 |
|
| 285 |
if changed and not module.check_mode: |
| 286 |
module.atomic_move(tempname, module.params['path']) |
| 287 |
|
| 288 |
if os.path.exists(tempname): |
| 289 |
os.remove(tempname) |
| 290 |
|
| 291 |
return changed |
| 292 |
|
| 293 |
except Exception as e: |
| 294 |
module.fail_json(msg="Failure downloading manifest, {0}".format(to_native(e))) |
| 295 |
|
| 296 |
|
| 297 |
def main(): |
| 298 |
module = AnsibleModule( |
| 299 |
argument_spec=dict( |
| 300 |
name=dict(type='str'), |
| 301 |
uuid=dict(type='str'), |
| 302 |
username=dict(required=True, no_log=True), |
| 303 |
password=dict(required=True, no_log=True), |
| 304 |
content_access_mode=dict(choices=['org_environment', 'entitlement'], default='entitlement'), |
| 305 |
pool_id=dict(type='str'), |
| 306 |
quantity=dict(type='int', default=1), |
| 307 |
pool_state=dict(choices=['present', 'absent'], default='present'), |
| 308 |
state=dict(choices=['present', 'absent'], default='present'), |
| 309 |
path=dict(type='path'), |
| 310 |
validate_certs=dict(default=True, type='bool'), |
| 311 |
portal=dict(default='https://subscription.rhsm.redhat.com'), |
| 312 |
), |
| 313 |
required_one_of=[['name', 'uuid']], |
| 314 |
supports_check_mode=True, |
| 315 |
) |
| 316 |
|
| 317 |
if module.params['validate_certs'] and not os.path.exists(REDHAT_UEP): |
| 318 |
module.warn("Couldn't find the Red Hat Entitlement Platform CA certificate ({0}) on your system. " |
| 319 |
"It's required to validate the certificate of {1}.".format(REDHAT_UEP, module.params['portal'])) |
| 320 |
|
| 321 |
username = module.params['username'] |
| 322 |
password = module.params['password'] |
| 323 |
|
| 324 |
|
| 325 |
module.params['url_username'] = username |
| 326 |
module.params['url_password'] = password |
| 327 |
module.params['force_basic_auth'] = True |
| 328 |
|
| 329 |
module.params['rhsm_owner'] = get_owner(module) |
| 330 |
|
| 331 |
manifest, man_changed = get_manifest(module) |
| 332 |
if module.params['pool_id'] and manifest: |
| 333 |
sub_changed = get_remove_or_attach_sub(module, manifest) |
| 334 |
else: |
| 335 |
sub_changed = False |
| 336 |
|
| 337 |
if module.params['path'] and manifest: |
| 338 |
export_changed = export_manifest(module, manifest) |
| 339 |
else: |
| 340 |
export_changed = False |
| 341 |
|
| 342 |
if manifest: |
| 343 |
manifest_uuid = manifest.get('uuid') |
| 344 |
else: |
| 345 |
manifest_uuid = None |
| 346 |
|
| 347 |
changed = man_changed or sub_changed or export_changed |
| 348 |
module.exit_json(changed=changed, uuid=manifest_uuid) |
| 349 |
|
| 350 |
|
| 351 |
if __name__ == '__main__': |
| 352 |
main() |