From e3c18284775b92990a51dcaaa7c614c532216541 Mon Sep 17 00:00:00 2001 From: Stefan Horning Date: Tue, 23 Feb 2021 13:33:04 +0100 Subject: [PATCH 1/3] Fix ec2_launch_template to correctly remove all None types, fixes issue #230 --- plugins/modules/ec2_launch_template.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/modules/ec2_launch_template.py b/plugins/modules/ec2_launch_template.py index 4a35812cfb4..d6a87feca3b 100644 --- a/plugins/modules/ec2_launch_template.py +++ b/plugins/modules/ec2_launch_template.py @@ -481,11 +481,22 @@ def delete_template(module): return {'changed': False} +def remove_none(obj): + if isinstance(obj, (list, tuple, set)): + return type(obj)(remove_none(x) for x in obj if x is not None) + elif isinstance(obj, dict): + return type(obj)((remove_none(k), remove_none(v)) + for k, v in obj.items() if k is not None and v is not None) + else: + return obj + + def create_or_update(module, template_options): ec2 = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff(catch_extra_error_codes=['InvalidLaunchTemplateId.NotFound'])) template, template_versions = existing_templates(module) out = {} lt_data = params_to_launch_data(module, dict((k, v) for k, v in module.params.items() if k in template_options)) + lt_data = remove_none(lt_data) if not (template or template_versions): # create a full new one try: From 5f32077190167071fa36a31d142e3cbf5804d964 Mon Sep 17 00:00:00 2001 From: Stefan Horning Date: Tue, 23 Feb 2021 13:46:55 +0100 Subject: [PATCH 2/3] Added changelog fragment for ec2_launch_template fix --- changelogs/fragments/438-ec2-launch-template-dict-fix.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/438-ec2-launch-template-dict-fix.yml diff --git a/changelogs/fragments/438-ec2-launch-template-dict-fix.yml b/changelogs/fragments/438-ec2-launch-template-dict-fix.yml new file mode 100644 index 00000000000..5e4592c2aa6 --- /dev/null +++ b/changelogs/fragments/438-ec2-launch-template-dict-fix.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - Remove None types from dict in ec2_launch_template before passing them wrongly to boto create method. From 8d2f9512db65f42f79a777d821c2e6b4821c4c0d Mon Sep 17 00:00:00 2001 From: Stefan Horning Date: Tue, 23 Feb 2021 13:50:37 +0100 Subject: [PATCH 3/3] Fix indentation of added method --- plugins/modules/ec2_launch_template.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/modules/ec2_launch_template.py b/plugins/modules/ec2_launch_template.py index d6a87feca3b..002dea9d62f 100644 --- a/plugins/modules/ec2_launch_template.py +++ b/plugins/modules/ec2_launch_template.py @@ -482,13 +482,13 @@ def delete_template(module): def remove_none(obj): - if isinstance(obj, (list, tuple, set)): - return type(obj)(remove_none(x) for x in obj if x is not None) - elif isinstance(obj, dict): - return type(obj)((remove_none(k), remove_none(v)) - for k, v in obj.items() if k is not None and v is not None) - else: - return obj + if isinstance(obj, (list, tuple, set)): + return type(obj)(remove_none(x) for x in obj if x is not None) + elif isinstance(obj, dict): + return type(obj)((remove_none(k), remove_none(v)) + for k, v in obj.items() if k is not None and v is not None) + else: + return obj def create_or_update(module, template_options):