# Buggy code
def _ensure_listeners_default_action_has_arn(self, listeners):
    """
    If a listener DefaultAction has been passed with a Target Group Name instead of ARN, lookup the ARN and
    replace the name.

    :param listeners: a list of listener dicts
    :return: the same list of dicts ensuring that each listener DefaultActions dict has TargetGroupArn key. If a TargetGroupName key exists, it is removed.
    """

    if not listeners:
        listeners = []

    for listener in listeners:
        if "TargetGroupName" in listener["DefaultActions"][0]:
            listener["DefaultActions"][0]["TargetGroupArn"] = convert_tg_name_to_arn(
                self.connection,
                self.module,
                listener["DefaultActions"][0]["TargetGroupName"],
            )
            del listener["DefaultActions"][0]["TargetGroupName"]

    return listeners


# Traceback (KeyError)
TASK [create alb] *********************************************************************************************************************************************************************
task path: /home/jevers/misc_dev/ansible_alb_test/alb.yml:63
Using module file /home/jevers/.local/lib/python3.6/site-packages/ansible/modules/cloud/amazon/elb_application_lb.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: jevers
<localhost> EXEC /bin/sh -c 'python3 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 1028, in <module>
main()
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 1023, in main
create_or_update_elb(connection, connection_ec2, module)
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 915, in create_or_update_elb
listener_changed = create_or_update_elb_listeners(connection, module, elb)
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 762, in create_or_update_elb_listeners
rules_to_add, rules_to_modify, rules_to_delete = compare_rules(connection, module, current_listeners, listener)
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 693, in compare_rules
modified_rule = compare_rule(current_rule, new_rule)
File "/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py", line 600, in compare_rule
if current_rule['Actions'][0]['TargetGroupArn'] != new_rule['Actions'][0]['TargetGroupArn']:
KeyError: 'TargetGroupArn'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 1028, in <module>\n    main()\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 1023, in main\n    create_or_update_elb(connection, connection_ec2, module)\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 915, in create_or_update_elb\n    listener_changed = create_or_update_elb_listeners(connection, module, elb)\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 762, in create_or_update_elb_listeners\n    rules_to_add, rules_to_modify, rules_to_delete = compare_rules(connection, module, current_listeners, listener)\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 693, in compare_rules\n    modified_rule = compare_rule(current_rule, new_rule)\n  File \"/tmp/ansible_7qen__0n/ansible_module_elb_application_lb.py\", line 600, in compare_rule\n    if current_rule['Actions'][0]['TargetGroupArn'] != new_rule['Actions'][0]['TargetGroupArn']:\nKeyError: 'TargetGroupArn'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def _ensure_listeners_default_action_has_arn(self, listeners):
    """
    If a listener DefaultAction has been passed with a Target Group Name instead of ARN, lookup the ARN and
    replace the name.

    :param listeners: a list of listener dicts
    :return: the same list of dicts ensuring that each listener DefaultActions dict has TargetGroupArn key. If a TargetGroupName key exists, it is removed.
    """

    if not listeners:
        listeners = []

    fixed_listeners = []
    for listener in listeners:
        fixed_actions = []
        for action in listener["DefaultActions"]:
            if "TargetGroupName" in action:
                action["TargetGroupArn"] = convert_tg_name_to_arn(
                    self.connection, self.module, action["TargetGroupName"]
                )
                del action["TargetGroupName"]
            fixed_actions.append(action)
        listener["DefaultActions"] = fixed_actions
        fixed_listeners.append(listener)

    return fixed_listeners