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 }}"