Skip to content

Commit

Permalink
Merge branch 'ansible-collections:main' into dev-eks_fargate_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
tjarra authored Feb 17, 2022
2 parents feefcf9 + d0596e3 commit 8c6e449
Show file tree
Hide file tree
Showing 8 changed files with 1,443 additions and 1,006 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/936-stabilize-ec2-eip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_eip - refactor module by fixing check_mode and more clear return obj. added integration tests (https://github.com/ansible-collections/community.aws/pull/936)
54 changes: 35 additions & 19 deletions plugins/modules/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
public_ip:
description:
- The IP address of a previously allocated EIP.
- When I(public_ip=present) and device is specified, the EIP is associated with the device.
- When I(public_ip=absent) and device is specified, the EIP is disassociated from the device.
- When I(state=present) and device is specified, the EIP is associated with the device.
- When I(state=absent) and device is specified, the EIP is disassociated from the device.
aliases: [ ip ]
type: str
state:
Expand Down Expand Up @@ -328,7 +328,7 @@ def find_address(ec2, module, public_ip, device_id, is_instance=True):
except is_boto3_error_code('InvalidAddress.NotFound') as e:
# If we're releasing and we can't find it, it's already gone...
if module.params.get('state') == 'absent':
module.exit_json(changed=False)
module.exit_json(changed=False, disassociated=False, released=False)
module.fail_json_aws(e, msg="Couldn't obtain list of existing Elastic IP addresses")

addresses = addresses["Addresses"]
Expand Down Expand Up @@ -385,6 +385,8 @@ def allocate_address(ec2, module, domain, reuse_existing_ip_allowed, check_mode,
return allocate_address_from_pool(ec2, module, domain, check_mode, public_ipv4_pool), True

try:
if check_mode:
return None, True
result = ec2.allocate_address(Domain=domain, aws_retry=True), True
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Couldn't allocate Elastic IP address")
Expand Down Expand Up @@ -493,8 +495,11 @@ def ensure_absent(ec2, module, address, device_id, check_mode, is_instance=True)


def allocate_address_from_pool(ec2, module, domain, check_mode, public_ipv4_pool):
# type: (EC2Connection, str, bool, str) -> Address
# type: (EC2Connection, AnsibleAWSModule, str, bool, str) -> Address
""" Overrides botocore's allocate_address function to support BYOIP """
if check_mode:
return None

params = {}

if domain is not None:
Expand All @@ -503,9 +508,6 @@ def allocate_address_from_pool(ec2, module, domain, check_mode, public_ipv4_pool
if public_ipv4_pool is not None:
params['PublicIpv4Pool'] = public_ipv4_pool

if check_mode:
params['DryRun'] = 'true'

try:
result = ec2.allocate_address(aws_retry=True, **params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
Expand Down Expand Up @@ -606,19 +608,33 @@ def main():
reuse_existing_ip_allowed, allow_reassociation,
module.check_mode, is_instance=is_instance
)
if 'allocation_id' not in result:
# Don't check tags on check_mode here - no EIP to pass through
module.exit_json(**result)
else:
if address:
changed = False
result = {
'changed': False,
'public_ip': address['PublicIp'],
'allocation_id': address['AllocationId']
}
else:
address, changed = allocate_address(
ec2, module, domain, reuse_existing_ip_allowed,
module.check_mode, tag_dict, public_ipv4_pool
)
result = {
'changed': changed,
'public_ip': address['PublicIp'],
'allocation_id': address['AllocationId']
}
if address:
result = {
'changed': changed,
'public_ip': address['PublicIp'],
'allocation_id': address['AllocationId']
}
else:
# Don't check tags on check_mode here - no EIP to pass through
result = {
'changed': changed
}
module.exit_json(**result)

result['changed'] |= ensure_ec2_tags(
ec2, module, result['allocation_id'],
Expand All @@ -633,21 +649,21 @@ def main():
released = release_address(ec2, module, address, module.check_mode)
result = {
'changed': True,
'disassociated': disassociated,
'released': released
'disassociated': disassociated['changed'],
'released': released['changed']
}
else:
result = {
'changed': disassociated['changed'],
'disassociated': disassociated,
'released': {'changed': False}
'disassociated': disassociated['changed'],
'released': False
}
else:
released = release_address(ec2, module, address, module.check_mode)
result = {
'changed': released['changed'],
'disassociated': {'changed': False},
'released': released
'disassociated': False,
'released': released['changed']
}

except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/ec2_eip_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
register: my_vm_eips
- ansible.builtin.debug:
msg: "{{ my_vm_eips.addresses | json_query(\"[?private_ip_address=='10.0.0.5']\") }}"
msg: "{{ my_vm_eips.addresses | selectattr('private_ip_address', 'equalto', '10.0.0.5') }}"
- name: List all EIP addresses for several VMs.
community.aws.ec2_eip_info:
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/targets/ec2_asg/aliases
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# reason: slow
# Tests take around 30 minutes
# reason: broken
disabled

slow
cloud/aws
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_asg/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# defaults file for ec2_asg
# Amazon Linux 2 AMI 2019.06.12 (HVM), GP2 Volume Type
ec2_ami_name: 'amzn2-ami-hvm-2.0.20190612-x86_64-gp2'
load_balancer_name: "{{ tiny_prefix }}-lb"
86 changes: 17 additions & 69 deletions tests/integration/targets/ec2_asg/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@
- set_fact:
ec2_ami_image: '{{ ec2_amis.images[0].image_id }}'

- name: load balancer name has to be less than 32 characters
# the 8 digit identifier at the end of resource_prefix helps determine during which test something
# was created
set_fact:
load_balancer_name: "{{ item }}-lb"
loop: "{{ resource_prefix | regex_findall('.{8}$') }}"

# Set up the testing dependencies: VPC, subnet, security group, and two launch configurations

- name: Create VPC for use in testing
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
Expand Down Expand Up @@ -239,8 +231,6 @@
that:
- output is not changed

# - name: pause for a bit to make sure that the group can't be trivially deleted
# pause: seconds=30
- name: kill asg
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down Expand Up @@ -349,7 +339,6 @@
# ============================================================

# grow scaling group to 3

- name: add 2 more instances wait for instances to be deemed healthy (ELB)
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand All @@ -373,7 +362,6 @@
# ============================================================

# Test max_instance_lifetime option

- name: enable asg max_instance_lifetime
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down Expand Up @@ -409,8 +397,7 @@

# ============================================================

# # perform rolling replace with different launch configuration

# perform rolling replace with different launch configuration
- name: perform rolling update to new AMI
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down Expand Up @@ -442,7 +429,6 @@
# ============================================================

# perform rolling replace with the original launch configuration

- name: perform rolling update to new AMI while removing the load balancer
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down Expand Up @@ -475,10 +461,6 @@
# ============================================================

# perform rolling replace with new launch configuration and lc_check:false

# Note - this is done async so we can query asg_facts during
# the execution. Issues #28087 and #35993 result in correct
# end result, but spin up extraneous instances during execution.
- name: "perform rolling update to new AMI with lc_check: false"
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand All @@ -496,32 +478,18 @@
lc_check: false
wait_timeout: 1800
state: present
async: 1800
poll: 0
register: asg_job

- name: get ec2_asg info for 3 minutes
# Collect ec2_asg_info
- name: get ec2_asg info
ec2_asg_info:
name: "{{ resource_prefix }}-asg"
register: output
loop_control:
pause: 15
loop: "{{ range(12) | list }}"

# Since we started with 3 servers and replace all of them.
# We should see 6 servers total.
# Since we started with 3 instances and replace all of them.
# We should see only 3 instances total.
- assert:
that:
- output | community.general.json_query(inst_id_json_query) | unique | length == 6
vars:
inst_id_json_query: results[].results[].instances[].instance_id

- name: Ensure ec2_asg task completes
async_status: jid="{{ asg_job.ansible_job_id }}"
register: status
until: status is finished
retries: 200
delay: 15
- output.results[0].instances | length == 3

# ============================================================

Expand All @@ -533,10 +501,6 @@
async: 400

# Create new asg with replace_all_instances and lc_check:false

# Note - this is done async so we can query asg_facts during
# the execution. Issues #28087 results in correct
# end result, but spin up extraneous instances during execution.
- name: "new asg with lc_check: false"
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand All @@ -554,33 +518,18 @@
lc_check: false
wait_timeout: 1800
state: present
async: 1800
poll: 0
register: asg_job

# Collect ec2_asg_info for 3 minutes
# Collect ec2_asg_info
- name: get ec2_asg information
ec2_asg_info:
name: "{{ resource_prefix }}-asg"
register: output
loop_control:
pause: 15
loop: "{{ range(12) | list }}"

# Get all instance_ids we saw and assert we saw number expected
# Should only see 3 (don't replace instances we just created)
- assert:
that:
- output | community.general.json_query(inst_id_json_query) | unique | length == 3
vars:
inst_id_json_query: results[].results[].instances[].instance_id

- name: Ensure ec2_asg task completes
async_status: jid="{{ asg_job.ansible_job_id }}"
register: status
until: status is finished
retries: 200
delay: 15
- output.results[0].instances | length == 3

# we need a launch template, otherwise we cannot test the mixed instance policy
- name: create launch template for autoscaling group to test its mixed instances policy
Expand Down Expand Up @@ -610,15 +559,15 @@
mixed_instances_policy:
instance_types:
- t3.micro
- t3a.micro
- t2.nano
wait_for_instances: yes
register: output

- assert:
that:
- "output.mixed_instances_policy | length == 2"
- "output.mixed_instances_policy[0] == 't3.micro'"
- "output.mixed_instances_policy[1] == 't3a.micro'"
- "output.mixed_instances_policy[1] == 't2.nano'"

- name: update autoscaling group with mixed-instances policy with instances_distribution
ec2_asg:
Expand All @@ -633,7 +582,7 @@
mixed_instances_policy:
instance_types:
- t3.micro
- t3a.micro
- t2.nano
instances_distribution:
on_demand_percentage_above_base_capacity: 0
spot_allocation_strategy: capacity-optimized
Expand All @@ -643,7 +592,7 @@
- assert:
that:
- "output.mixed_instances_policy_full['launch_template']['overrides'][0]['instance_type'] == 't3.micro'"
- "output.mixed_instances_policy_full['launch_template']['overrides'][1]['instance_type'] == 't3a.micro'"
- "output.mixed_instances_policy_full['launch_template']['overrides'][1]['instance_type'] == 't2.nano'"
- "output.mixed_instances_policy_full['instances_distribution']['on_demand_percentage_above_base_capacity'] == 0"
- "output.mixed_instances_policy_full['instances_distribution']['spot_allocation_strategy'] == 'capacity-optimized'"

Expand Down Expand Up @@ -683,7 +632,7 @@
- name: update autoscaling group with tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
Expand All @@ -701,7 +650,7 @@
- name: update autoscaling group add tg2
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
Expand All @@ -720,7 +669,7 @@
- name: update autoscaling group remove tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg2.target_group_arn }}"
Expand All @@ -739,7 +688,7 @@
- name: update autoscaling group remove tg2 and add tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
Expand All @@ -758,7 +707,7 @@
- name: target group no change
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
Expand Down Expand Up @@ -789,7 +738,6 @@
retries: 10

# Remove the testing dependencies

- name: remove target group
elb_target_group:
name: "{{ item }}"
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/targets/ec2_eip/aliases
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# https://github.com/ansible-collections/community.aws/issues/159
unstable
# unstable

cloud/aws
ec2_eip_info
Loading

0 comments on commit 8c6e449

Please sign in to comment.