diff --git a/changelogs/fragments/1736-elb_target_group_property.yml b/changelogs/fragments/1736-elb_target_group_property.yml new file mode 100644 index 00000000000..7a760816add --- /dev/null +++ b/changelogs/fragments/1736-elb_target_group_property.yml @@ -0,0 +1,2 @@ +bugfixes: + - elb_target_group - ensure ``AvailabilityZone`` is kept in target definitions when ``Id`` and ``Port`` are passed (https://github.com/ansible-collections/community.aws/issues/1736). diff --git a/plugins/modules/elb_target_group.py b/plugins/modules/elb_target_group.py index f9849264200..854876e0710 100644 --- a/plugins/modules/elb_target_group.py +++ b/plugins/modules/elb_target_group.py @@ -729,9 +729,12 @@ def create_or_update_target_group(connection, module): if add_instances: instances_to_add = [] - for target in params['Targets']: - if target['Id'] in add_instances: - instances_to_add.append({'Id': target['Id'], 'Port': target['Port']}) + for target in params["Targets"]: + if target["Id"] in add_instances: + tmp_item = {"Id": target["Id"], "Port": target["Port"]} + if target.get("AvailabilityZone"): + tmp_item["AvailabilityZone"] = target["AvailabilityZone"] + instances_to_add.append(tmp_item) changed = True try: diff --git a/tests/integration/targets/elb_target/tasks/ec2_target.yml b/tests/integration/targets/elb_target/tasks/ec2_target.yml index 4a99c41a8b0..611aca26f13 100644 --- a/tests/integration/targets/elb_target/tasks/ec2_target.yml +++ b/tests/integration/targets/elb_target/tasks/ec2_target.yml @@ -168,14 +168,8 @@ tags: Name: "{{ resource_prefix }}-inst" user_data: | - #cloud-config - package_upgrade: true - package_update: true - packages: - - httpd - runcmd: - - "service httpd start" - - echo "HELLO ANSIBLE" > /var/www/html/index.html + #!/bin/bash + sudo nohup python3 -m http.server 80 & register: ec2 - set_fact: @@ -480,6 +474,46 @@ - not result.changed - not result.target_health_descriptions + - name: create ip target group + elb_target_group: + name: "{{ tg_name }}-ip" + health_check_port: 443 + protocol: tcp + port: 443 + vpc_id: "{{ vpc.vpc.id }}" + state: present + target_type: ip + register: result + + - name: ip target group must be created + assert: + that: + - result.changed + - result.target_type == 'ip' + + - name: "mobify ip target group with AvailabilityZone: all" + elb_target_group: + name: "{{ tg_name }}-ip" + health_check_port: 443 + protocol: tcp + port: 443 + vpc_id: "{{ vpc.vpc.id }}" + state: present + target_type: ip + wait: false + modify_targets: true + targets: + - Id: 192.168.178.32 + Port: 443 + AvailabilityZone: all + register: result + + - name: ip target group must be modified + assert: + that: + - result.changed + - result.load_balancing_cross_zone_enabled == 'use_load_balancer_configuration' + # ============================================================ always: @@ -533,6 +567,7 @@ - "{{ tg_used_name }}" - "{{ tg_tcpudp_name }}" - "{{ tg_name }}-nlb" + - "{{ tg_name }}-ip" ignore_errors: true - name: remove routing rules