From 26154d22d3308f50be0e13e804e1ca7e752018bc Mon Sep 17 00:00:00 2001 From: Graham Mainwaring Date: Tue, 30 Mar 2021 17:05:06 -0400 Subject: [PATCH 1/3] Make tower_license module idempotent Signed-off-by: Graham Mainwaring --- .../plugins/modules/tower_license.py | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/modules/tower_license.py b/awx_collection/plugins/modules/tower_license.py index b751f4fe7d..49542c0256 100644 --- a/awx_collection/plugins/modules/tower_license.py +++ b/awx_collection/plugins/modules/tower_license.py @@ -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 ''' @@ -55,7 +62,7 @@ def main(): ), ) - json_output = {'changed': True} + json_output = {} 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 +72,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 = (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) From caec34737195751c623797d25a4bc1ca57d18426 Mon Sep 17 00:00:00 2001 From: Graham Mainwaring Date: Wed, 31 Mar 2021 17:03:09 -0400 Subject: [PATCH 2/3] Make sure changed is always set --- awx_collection/plugins/modules/tower_license.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/modules/tower_license.py b/awx_collection/plugins/modules/tower_license.py index 49542c0256..9d3bd717f3 100644 --- a/awx_collection/plugins/modules/tower_license.py +++ b/awx_collection/plugins/modules/tower_license.py @@ -33,7 +33,7 @@ options: 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. + unlicensed or trial licensed. When force=true, the license is always applied. required: True type: bool default: 'False' @@ -62,7 +62,7 @@ def main(): ), ) - json_output = {} + 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') @@ -83,7 +83,7 @@ def main(): ) # Determine if we will install the license - perform_install = (not already_licensed) or module.params.get('force') + perform_install = bool((not already_licensed) or module.params.get('force')) # Handle check mode if module.check_mode: From b3d1c7ff9f3138c60aa289fa321de855f079226e Mon Sep 17 00:00:00 2001 From: Graham Mainwaring Date: Thu, 1 Apr 2021 20:47:30 -0400 Subject: [PATCH 3/3] Add force parameter to argument_spec --- awx_collection/plugins/modules/tower_license.py | 1 + 1 file changed, 1 insertion(+) diff --git a/awx_collection/plugins/modules/tower_license.py b/awx_collection/plugins/modules/tower_license.py index 9d3bd717f3..37ce8531ff 100644 --- a/awx_collection/plugins/modules/tower_license.py +++ b/awx_collection/plugins/modules/tower_license.py @@ -59,6 +59,7 @@ def main(): argument_spec=dict( manifest=dict(type='str', required=True), eula_accepted=dict(type='bool', required=True), + force=dict(type='bool', required=False), ), )