Merge pull request #9759 from ghjm/idempotence_is_a_thing

Make tower_license module idempotent

SUMMARY
Currently, the tower_license module always installs a license, without checking if Tower is already licensed.  In general, Ansible modules are supposed to be idempotent.  This PR updates the module to check whether Tower is already licensed, and do nothing in that case.  A new parameter force is provided so the user can always install the license, or override an existing license with a new one.
ISSUE TYPE
Bughancement
COMPONENT NAME
awx_collection
AWX VERSION
awx: 18.0.0
(but really Tower 3.8.2)

Reviewed-by: Shane McDonald <me@shanemcd.com>
Reviewed-by: Christian Adams <rooftopcellist@gmail.com>
Reviewed-by: Alan Rominger <arominge@redhat.com>
Reviewed-by: Graham Mainwaring <graham@mhn.org>
Reviewed-by: Bianca Henderson <beeankha@gmail.com>
This commit is contained in:
softwarefactory-project-zuul[bot] 2021-05-12 17:44:47 +00:00 committed by GitHub
commit 71f9476a51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,13 @@ options:
- Whether or not the EULA is accepted.
required: True
type: bool
force:
description:
- By default, the license manifest will only be applied if Tower is currently
unlicensed or trial licensed. When force=true, the license is always applied.
required: True
type: bool
default: 'False'
extends_documentation_fragment: awx.awx.auth
'''
@ -52,10 +59,11 @@ def main():
argument_spec=dict(
manifest=dict(type='str', required=True),
eula_accepted=dict(type='bool', required=True),
force=dict(type='bool', required=False),
),
)
json_output = {'changed': True}
json_output = {'changed': False}
if not module.params.get('eula_accepted'):
module.fail_json(msg='You must accept the EULA by passing in the param eula_accepted as True')
@ -65,11 +73,28 @@ def main():
except OSError as e:
module.fail_json(msg=str(e))
# Deal with check mode
# Check if Tower is already licensed
config = module.get_endpoint('config')['json']
already_licensed = (
'license_info' in config
and 'instance_count' in config['license_info']
and config['license_info']['instance_count'] > 0
and 'trial' in config['license_info']
and not config['license_info']['trial']
)
# Determine if we will install the license
perform_install = bool((not already_licensed) or module.params.get('force'))
# Handle check mode
if module.check_mode:
json_output['changed'] = perform_install
module.exit_json(**json_output)
module.post_endpoint('config', data={'eula_accepted': True, 'manifest': manifest.decode()})
# Do the actual install, if we need to
if perform_install:
json_output['changed'] = True
module.post_endpoint('config', data={'eula_accepted': True, 'manifest': manifest.decode()})
module.exit_json(**json_output)