# Buggy code
def __init__(self, results, runner):
    self.runner = runner

    self.results = {"contacted": {}, "dark": {}}
    self.hosts_to_poll = []
    self.completed = False

    # flag to determine if at least one host was contacted
    self.active = False
    # True to work with the `and` below
    skipped = True
    jid = None
    for host, res in results["contacted"].iteritems():
        if res.get("started", False):
            self.hosts_to_poll.append(host)
            jid = res.get("ansible_job_id", None)
            self.runner.vars_cache[host]["ansible_job_id"] = jid
            self.active = True
        else:
            skipped = skipped and res.get("skipped", False)
            self.results["contacted"][host] = res
    for host, res in results["dark"].iteritems():
        self.runner.vars_cache[host]["ansible_job_id"] = ""
        self.results["dark"][host] = res

    if not skipped:
        if jid is None:
            raise errors.AnsibleError("unexpected error: unable to determine jid")
        if len(self.hosts_to_poll) == 0:
            raise errors.AnsibleError("unexpected error: no hosts to poll")


# Traceback (KeyError)
TASK: [a local task] **********************************************************
skipping: [localhost]
Traceback (most recent call last):
File "/Users/jyoung/lib/ansible-main/bin/ansible-playbook", line 317, in <module>
sys.exit(main(sys.argv[1:]))
File "/Users/jyoung/lib/ansible-main/bin/ansible-playbook", line 257, in main
pb.run()
File "/Users/jyoung/lib/ansible-main/lib/ansible/playbook/__init__.py", line 319, in run
if not self._run_play(play):
File "/Users/jyoung/lib/ansible-main/lib/ansible/playbook/__init__.py", line 673, in _run_play
if not self._run_task(play, task, False):
File "/Users/jyoung/lib/ansible-main/lib/ansible/playbook/__init__.py", line 449, in _run_task
results = self._run_task_internal(task)
File "/Users/jyoung/lib/ansible-main/lib/ansible/playbook/__init__.py", line 413, in _run_task_internal
self.runner_callbacks.on_async_ok(host, res, poller.runner.vars_cache[host]['ansible_job_id'])
KeyError: 'ansible_job_id'

# Fixed code
def __init__(self, results, runner):
    self.runner = runner

    self.results = {"contacted": {}, "dark": {}}
    self.hosts_to_poll = []
    self.completed = False

    # flag to determine if at least one host was contacted
    self.active = False
    # True to work with the `and` below
    skipped = True
    jid = None
    for host, res in results["contacted"].iteritems():
        if res.get("started", False):
            self.hosts_to_poll.append(host)
            jid = res.get("ansible_job_id", None)
            self.runner.vars_cache[host]["ansible_job_id"] = jid
            self.active = True
        else:
            skipped = skipped and res.get("skipped", False)
            self.runner.vars_cache[host]["ansible_job_id"] = ""
            self.results["contacted"][host] = res
    for host, res in results["dark"].iteritems():
        self.runner.vars_cache[host]["ansible_job_id"] = ""
        self.results["dark"][host] = res

    if not skipped:
        if jid is None:
            raise errors.AnsibleError("unexpected error: unable to determine jid")
        if len(self.hosts_to_poll) == 0:
            raise errors.AnsibleError("unexpected error: no hosts to poll")