From b55f20a3ee7c9e21fdcc7522961e70d0416af2ee Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 11 Jan 2020 02:53:48 +0100 Subject: [PATCH] [2.9] various elbv2 bugfixes (#65212) * Add aws elb multiple host header support (#65021) * Add support for aws elbv2 multiple host header actions * Add example of multiple host header * Add test for multiple host headers * Add missing quote * Update test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml Co-Authored-By: Jill R <4121322+jillr@users.noreply.github.com> * Update test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml Co-Authored-By: Jill R <4121322+jillr@users.noreply.github.com> * Remove debug statements (cherry picked from commit d52af75c68b3f31c994d8b234b9c1e2387f9a4dd) * elbv2 - print() statement results an task failure, even if the task itself was successfull (#65183) (cherry picked from commit cba9abcbec6d9d11efea7b38f997e4ed16ba7687) * Add changelog fragment. * Update changelogs/fragments/65021-65183-elb-bugs.yml * Force values to be sorted when compared. (#65315) * Force values to be sorted when compared. This avoids problems where lists of values could be generated in indeterminate order. * Update lib/ansible/module_utils/aws/elbv2.py Thank you felixfontein, sorry I missed this detail. Co-Authored-By: Felix Fontein (cherry picked from commit 777b6d3e3098cbcf9f7e1f6d1b8f5fe04db5cf64) * Update changelog. Co-authored-by: Michael Mayer Co-authored-by: Markus Bergholz Co-authored-by: roberbri --- changelogs/fragments/65021-65183-elb-bugs.yml | 3 + lib/ansible/module_utils/aws/elbv2.py | 10 +-- .../cloud/amazon/elb_application_lb.py | 9 +++ .../tasks/test_modifying_alb_listeners.yml | 63 +++++++++++++++++++ 4 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/65021-65183-elb-bugs.yml diff --git a/changelogs/fragments/65021-65183-elb-bugs.yml b/changelogs/fragments/65021-65183-elb-bugs.yml new file mode 100644 index 00000000000..5eafd883bb7 --- /dev/null +++ b/changelogs/fragments/65021-65183-elb-bugs.yml @@ -0,0 +1,3 @@ +bugfixes: +- "elb_application_lb, elb_network_lb - fixed errors during listener rule comparison which caused broken JSON, and which caused some values not being compared correctly. (https://github.com/ansible/ansible/issues/65020)" +- "elb_application_lb, elb_network_lb - idempotence check for rules now compares all values order-independently, instead of just comparing the first value if multiple are specified." diff --git a/lib/ansible/module_utils/aws/elbv2.py b/lib/ansible/module_utils/aws/elbv2.py index 34a428dfd96..ba61c6405af 100644 --- a/lib/ansible/module_utils/aws/elbv2.py +++ b/lib/ansible/module_utils/aws/elbv2.py @@ -717,7 +717,7 @@ class ELBListenerRules(object): current_condition['SourceIpConfig']['Values'][0] == condition['SourceIpConfig']['Values'][0]): condition_found = True break - elif current_condition['Field'] == condition['Field'] and current_condition['Values'][0] == condition['Values'][0]: + elif current_condition['Field'] == condition['Field'] and sorted(current_condition['Values']) == sorted(condition['Values']): condition_found = True break @@ -732,7 +732,7 @@ class ELBListenerRules(object): modified_rule = {} # Priority - if int(current_rule['Priority']) != new_rule['Priority']: + if int(current_rule['Priority']) != int(new_rule['Priority']): modified_rule['Priority'] = new_rule['Priority'] # Actions @@ -754,8 +754,6 @@ class ELBListenerRules(object): if len(current_rule['Actions']) == 1 and len(new_rule['Actions']) == 1: if current_rule['Actions'] != new_rule['Actions']: modified_rule['Actions'] = new_rule['Actions'] - print("modified_rule:") - print(new_rule['Actions']) # if actions have multiple elements, we'll have to order them first before comparing. # multiple actions will have an 'Order' key for this purpose else: @@ -776,13 +774,9 @@ class ELBListenerRules(object): if current_actions_sorted != new_actions_sorted_no_secret: modified_rule['Actions'] = new_rule['Actions'] - print("modified_rule:") - print(new_rule['Actions']) # If the action lengths are different, then replace with the new actions else: modified_rule['Actions'] = new_rule['Actions'] - print("modified_rule:") - print(new_rule['Actions']) # Conditions modified_conditions = [] diff --git a/lib/ansible/modules/cloud/amazon/elb_application_lb.py b/lib/ansible/modules/cloud/amazon/elb_application_lb.py index 3fa449d11a6..4ac16fdc430 100644 --- a/lib/ansible/modules/cloud/amazon/elb_application_lb.py +++ b/lib/ansible/modules/cloud/amazon/elb_application_lb.py @@ -237,6 +237,15 @@ EXAMPLES = ''' ContentType: "text/plain" MessageBody: "This is the page you're looking for" StatusCode: "200" + - Conditions: + - Field: host-header + Values: + - "hostname.domain.com" + - "alternate.domain.com" + Priority: '4' + Actions: + - TargetGroupName: test-target-group + Type: forward state: present # Remove an ELB diff --git a/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml b/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml index 31c1569168b..943d766340d 100644 --- a/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml +++ b/test/integration/targets/elb_application_lb/tasks/test_modifying_alb_listeners.yml @@ -118,6 +118,69 @@ - alb.changed - alb.listeners[0].rules|length == 3 + - name: add a rule that uses the host header condition to the listener + elb_application_lb: + name: "{{ alb_name }}" + subnets: "{{ alb_subnets }}" + security_groups: "{{ sec_group.group_id }}" + state: present + purge_rules: no + listeners: + - Protocol: HTTP + Port: 80 + DefaultActions: + - Type: forward + TargetGroupName: "{{ tg_name }}" + Rules: + - Conditions: + - Field: host-header + Values: + - 'local.mydomain.com' + Priority: '3' + Actions: + - TargetGroupName: "{{ tg_name }}" + Type: forward + <<: *aws_connection_info + register: alb + + - assert: + that: + - alb.changed + - alb.listeners[0].rules|length == 4 + - '{{ alb|json_query("listeners[].rules[].conditions[].host_header_config.values[]")|length == 1 }}' + + - name: test replacing the rule that uses the host header condition with multiple host header conditions + elb_application_lb: + name: "{{ alb_name }}" + subnets: "{{ alb_subnets }}" + security_groups: "{{ sec_group.group_id }}" + purge_rules: no + state: present + listeners: + - Protocol: HTTP + Port: 80 + DefaultActions: + - Type: forward + TargetGroupName: "{{ tg_name }}" + Rules: + - Conditions: + - Field: host-header + Values: + - 'local.mydomain.com' + - 'alternate.mydomain.com' + Priority: '3' + Actions: + - TargetGroupName: "{{ tg_name }}" + Type: forward + <<: *aws_connection_info + register: alb + + - assert: + that: + - alb.changed + - alb.listeners[0].rules|length == 4 + - '{{ alb|json_query("listeners[].rules[].conditions[].host_header_config.values[]")|length == 2 }}' + - name: remove the rule elb_application_lb: name: "{{ alb_name }}"