# Buggy code
def _compare_condition(self, current_conditions, condition):
    """

    :param current_conditions:
    :param condition:
    :return:
    """

    condition_found = False

    for current_condition in current_conditions:
        if (
            current_condition["Field"] == condition["Field"]
            and current_condition["Values"][0] == condition["Values"][0]
        ):
            condition_found = True
            break

    return condition_found


# 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 _compare_condition(self, current_conditions, condition):
    """

    :param current_conditions:
    :param condition:
    :return:
    """

    condition_found = False

    for current_condition in current_conditions:
        if current_condition.get("SourceIpConfig"):
            if (
                current_condition["Field"] == condition["Field"]
                and current_condition["SourceIpConfig"]["Values"][0]
                == condition["SourceIpConfig"]["Values"][0]
            ):
                condition_found = True
                break
        elif (
            current_condition["Field"] == condition["Field"]
            and current_condition["Values"][0] == condition["Values"][0]
        ):
            condition_found = True
            break

    return condition_found