Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for LaunchConfigName nor LaunchTemplate existing on ASG object #54692

Merged
merged 1 commit into from
Apr 8, 2019

Conversation

jwitko
Copy link
Contributor

@jwitko jwitko commented Apr 1, 2019

SUMMARY

When attempting to use ec2_asg to make changes to an already existing autoscaling group, the module fails.

Fixes #50562

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

ec2_asg.py

ADDITIONAL INFORMATION

I was hiting an error when running my playbooks to modify auto-scaling groups and discovered the existing issue #50562. Taking a further look I was able to get the following error message after adding a print debug line

The full traceback is:
Traceback (most recent call last):
  File "<stdin>", line 113, in <module>
  File "<stdin>", line 105, in _ansiballz_main
  File "<stdin>", line 48, in invoke_module
  File "/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py", line 1675, in <module>
  File "/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py", line 1661, in main
  File "/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py", line 1023, in create_autoscaling_group
  File "/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py", line 636, in get_properties
KeyError: 'LaunchTemplate'

failed: [localhost] (item=test-swarm-general-master-m5-large-master) => {
    "changed": false,
    "item": [
        {
            "associate_public_ip_address": false,
            "block_device_mappings": [],
            "classic_link_vpc_security_groups": [],
            "created_time": "2019-04-01 20:07:19.322000+00:00",
            "ebs_optimized": true,
            "iam_instance_profile": "test-swarm-general-role",
            "image_id": "ami-056d69b6d20f9f180",
            "instance_monitoring": false,
            "instance_type": "m5.large",
            "kernel_id": "",
            "key_name": "ansible-keypair",
            "launch_configuration_arn": "arn:aws:autoscaling:us-east-1:REDACTED:launchConfiguration:dd954e0f-8e3c-463c-b30b-41b1b698fbb0:launchConfigurationName/test-swarm-general-m5-large-master-20190401-1605",
            "launch_configuration_name": "test-swarm-general-m5-large-master-20190401-1605",
            "ramdisk_id": "",
            "security_groups": [
                "sg-02f86b76"
            ],
            "user_data": ""
        },
        {
            "alb_target_groups": [
                {
                    "alb_tg_health_check_interval": 120,
                    "alb_tg_health_check_path": "/v2/_catalog",
                    "alb_tg_health_check_port": 5000,
                    "alb_tg_health_check_protocol": "http",
                    "alb_tg_health_check_timeout": 15,
                    "alb_tg_healthy_threshold_count": 3,
                    "alb_tg_name": "us-east-1-test-dockerhub-5000",
                    "alb_tg_port": 5000,
                    "alb_tg_protocol": "http",
                    "alb_tg_return_code_matcher": 200,
                    "alb_tg_unhealthy_threshold_count": 3
                },
                {
                    "alb_tg_health_check_interval": 120,
                    "alb_tg_health_check_path": "/users/sign_in",
                    "alb_tg_health_check_port": "8085",
                    "alb_tg_health_check_protocol": "http",
                    "alb_tg_health_check_timeout": 15,
                    "alb_tg_healthy_threshold_count": 3,
                    "alb_tg_name": "test-swarm-general-gitlab",
                    "alb_tg_port": "8085",
                    "alb_tg_protocol": "http",
                    "alb_tg_return_code_matcher": 302,
                    "alb_tg_unhealthy_threshold_count": 3
                }
            ],
            "asg_alarm_arn": "arn:aws:sns:us-east-1:569557445448:loadbalancer_alarms",
            "asg_default_cooldown": 700,
            "asg_desired": 3,
            "asg_dimension": {
                "AutoScalingGroupName": "test-swarm-general-master"
            },
            "asg_evaluation_periods": 3,
            "asg_health_check_period": 300,
            "asg_max": 3,
            "asg_metric_namespace": "AWS/EC2",
            "asg_min": 3,
            "asg_period": 120,
            "asg_purpose": "master",
            "asg_scale_in_cooldown": 700,
            "asg_scale_metric": "CPUUtilization",
            "asg_scale_out_cooldown": 700,
            "asg_scalein_comparison": "<=",
            "asg_scalein_description": "This will alarm when CPU utilization is under 50% for 9 minutes",
            "asg_scalein_threshold": "30.0",
            "asg_scaleout_comparison": ">=",
            "asg_scaleout_description": "This will alarm when CPU utilization is over 80% for 9 minutes",
            "asg_scaleout_threshold": "80.0",
            "asg_statistic": "Average",
            "asg_unit": "Percent",
            "swarm_labels": [
                "swarm-general:true",
                "master:true"
            ],
            "type": "m5.large"
        }
    ],
    "module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 113, in <module>\n  File \"<stdin>\", line 105, in _ansiballz_main\n  File \"<stdin>\", line 48, in invoke_module\n  File \"/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py\", line 1675, in <module>\n  File \"/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py\", line 1661, in main\n  File \"/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py\", line 1023, in create_autoscaling_group\n  File \"/tmp/ansible_ec2_asg_payload_6Npffj/__main__.py\", line 636, in get_properties\nKeyError: 'LaunchTemplate'\n",
    "module_stdout": "TESTING: {u'InstanceId': 'i-007caa4fef0eeb33f', u'ProtectedFromScaleIn': False, u'AvailabilityZone': 'us-east-1d', u'HealthStatus': 'Healthy', u'LifecycleState': 'InService'}\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

The print out was from the section where the error was hitting:

        for i in autoscaling_group_instances:
            print "TESTING: " + str(i)
            if i.get('LaunchConfigurationName'):
                instance_facts[i['InstanceId']] = {'health_status': i['HealthStatus'],
                                                   'lifecycle_state': i['LifecycleState'],
                                                   'launch_config_name': i['LaunchConfigurationName']}
            else:
                instance_facts[i['InstanceId']] = {'health_status': i['HealthStatus'],
                                                   'lifecycle_state': i['LifecycleState'],
                                                   'launch_template': i['LaunchTemplate']}

It turns out as evidenced by "module_stdout": "TESTING: {u'InstanceId': 'i-007caa4fef0eeb33f', u'ProtectedFromScaleIn': False, u'AvailabilityZone': 'us-east-1d', u'HealthStatus': 'Healthy', u'LifecycleState': 'InService'}\n", that there are in fact times where this will return without either LaunchConfigurationName or LaunchTemplate defined. (The ASG in question has a LaunchConfig).

This PR adds a conditional to allow for neither to be defined and it does indeed seem to solve my issue without any unintended side-effects that I can tell.

@jwitko
Copy link
Contributor Author

jwitko commented Apr 1, 2019

@nwsparks Would love to hear from you if the above resolves your issue.

@ansibot
Copy link
Contributor

ansibot commented Apr 1, 2019

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 aws bug This issue/PR relates to a bug. cloud community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. labels Apr 1, 2019
@willthames
Copy link
Contributor

Are instances in this state on the way in or the way out of the ASG? (I can only imagine an instance not to have a launch config/template in one of those two scenarios). If they're on the way out we should probably just not put them in the instance_facts

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Apr 1, 2019
@jwitko
Copy link
Contributor Author

jwitko commented Apr 2, 2019

@willthames This was done on an ASG that was not in any transitional state. I dug a little deeper and I believe this is because in my organization we rotate and prune launch configs at a very fast pace. The instances that are in this ASG no longer have launch configurations attached to them because those launch configurations were deleted after the instances were created.

So the scenario this covers is if you have instances that were created and then later on their launch configurations were deleted (possible the same behavior for LaunchTemplate, I'm unsure)

@jwitko
Copy link
Contributor Author

jwitko commented Apr 8, 2019

@willthames checking if there is anything else I can provide for this PR?

@willthames
Copy link
Contributor

It would be good to improve theinstance_facts docs to reflect this but they don't detail every key already so the sample is still accurate most of the time

@willthames willthames merged commit 87f8e37 into ansible:devel Apr 8, 2019
@jwitko jwitko deleted the ec2_asg_invalid_key branch April 9, 2019 01:08
@ansible ansible locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 aws bug This issue/PR relates to a bug. cloud community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ec2_asg fails when existing group uses launch template instead of launch config
3 participants