# Buggy code
def _ensure_rules_action_has_arn(self, rules):
    """
    If a rule Action has been passed with a Target Group Name instead of ARN, lookup the ARN and
    replace the name.

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

    for rule in rules:
        if "TargetGroupName" in rule["Actions"][0]:
            rule["Actions"][0]["TargetGroupArn"] = convert_tg_name_to_arn(
                self.connection, self.module, rule["Actions"][0]["TargetGroupName"]
            )
            del rule["Actions"][0]["TargetGroupName"]

    return rules


# 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_rules_action_has_arn(self, rules):
    """
    If a rule Action has been passed with a Target Group Name instead of ARN, lookup the ARN and
    replace the name.

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

    fixed_rules = []
    for rule in rules:
        fixed_actions = []
        for action in rule["Actions"]:
            if "TargetGroupName" in action:
                action["TargetGroupArn"] = convert_tg_name_to_arn(
                    self.connection, self.module, action["TargetGroupName"]
                )
                del action["TargetGroupName"]
            fixed_actions.append(action)
        rule["Actions"] = fixed_actions
        fixed_rules.append(rule)

    return fixed_rules