From a488b3a8edb89fd144bcc7b9342694265ea6143f Mon Sep 17 00:00:00 2001 From: mjmayer Date: Wed, 25 Jul 2018 03:55:34 -0700 Subject: [PATCH] elb_application_lb purge rules option (#43113) * Add parameter to keep elb rules Does not purge elb rules. This is usefull if running the elb_application_lb role and there is the desire to keep existing rules. * Change variable name keep_rules to purge_rules The descriptor purge has been used in the past. * Changed default for purge_rules Default is purge_rules. This is how the module has functioned previously. This change maintains the previous behavior. * Add integration test for purge_rules flag * Change wording of test task * Fix merge conflcit * Changed default for purge_rules Default is purge_rules. This is how the module has functioned previously. This change maintains the previous behavior. * merge conflcit * Change wording of test task * Add purge_rules option to test * Change test description wording * Expand purge_rules documentation * Clarifies documentation for purge_rules option --- .../cloud/amazon/elb_application_lb.py | 18 +++++++---- .../tasks/test_modifying_alb_listeners.yml | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/elb_application_lb.py b/lib/ansible/modules/cloud/amazon/elb_application_lb.py index 3985e9c70b..f4edeb65f9 100644 --- a/lib/ansible/modules/cloud/amazon/elb_application_lb.py +++ b/lib/ansible/modules/cloud/amazon/elb_application_lb.py @@ -119,6 +119,12 @@ options: description: - The time in seconds to use in conjunction with I(wait). version_added: 2.6 + purge_rules: + description: + - When set to no, keep the existing load balancer rules in place. Will modify and add, but will not delete. + default: yes + type: bool + version_added: 2.7 extends_documentation_fragment: - aws - ec2 @@ -444,10 +450,11 @@ def create_or_update_elb(elb_obj): rules_to_add, rules_to_modify, rules_to_delete = rules_obj.compare_rules() # Delete rules - for rule in rules_to_delete: - rule_obj = ELBListenerRule(elb_obj.connection, elb_obj.module, {'RuleArn': rule}, rules_obj.listener_arn) - rule_obj.delete() - elb_obj.changed = True + if elb_obj.module.params['purge_rules']: + for rule in rules_to_delete: + rule_obj = ELBListenerRule(elb_obj.connection, elb_obj.module, {'RuleArn': rule}, rules_obj.listener_arn) + rule_obj.delete() + elb_obj.changed = True # Add rules for rule in rules_to_add: @@ -524,7 +531,8 @@ def main(): state=dict(choices=['present', 'absent'], type='str'), tags=dict(type='dict'), wait_timeout=dict(type='int'), - wait=dict(default=False, type='bool') + wait=dict(default=False, type='bool'), + purge_rules=dict(default=True, type='bool') ) ) 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 db05dab29b..31c1569168 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 @@ -88,6 +88,36 @@ - not alb.changed - alb.listeners[0].rules|length == 2 + - name: test a rule can be added and other rules will not be removed when purge_rules is no. + 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: path-pattern + Values: + - '/new' + Priority: '2' + Actions: + - TargetGroupName: "{{ tg_name }}" + Type: forward + <<: *aws_connection_info + register: alb + + - assert: + that: + - alb.changed + - alb.listeners[0].rules|length == 3 + - name: remove the rule elb_application_lb: name: "{{ alb_name }}"