# Buggy code
def reset(self):
    # If we have a persistent ssh connection (ControlPersist), we can ask it to stop listening.
    cmd = map(
        to_bytes,
        self._build_command(self._play_context.ssh_executable, "-O", "stop", self.host),
    )
    controlpersist, controlpath = self._persistence_controls(cmd)
    if controlpersist:
        p = subprocess.Popen(
            cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        stdout, stderr = p.communicate()
        display.vvv("sending stop: %s" % cmd)

    self.close()


# Traceback (AttributeError)
[vagrant@localhost infra-bootstrap]$ ansible-playbook --limit=core --inventory-file=/tmp/vagrant-ansible/inventory -vvv test.yml
Using /etc/ansible/ansible.cfg as config file
[WARNING]: Found both group and host with same name: core


PLAYBOOK: test.yml ***************************************************************************************************************************************************
1 plays in test.yml

PLAY [all] ***********************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<core> ESTABLISH LOCAL CONNECTION FOR USER: vagrant
<core> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<core> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807 `" &amp;&amp; echo ansible-tmp-1492207079.55-247174731112807="` echo /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807 `" ) &amp;&amp; sleep 0'
<core> PUT /tmp/tmpbuhXwS TO /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807/setup.py
<core> EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807/ /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807/setup.py &amp;&amp; sleep 0'
<core> EXEC /bin/sh -c '/usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807/setup.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1492207079.55-247174731112807/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [core]
META: ran handlers
ERROR! Unexpected Exception: 'NoneType' object has no attribute 'reset'
the full traceback was:

Traceback (most recent call last):
File "/usr/bin/ansible-playbook", line 109, in <module>
exit_code = cli.run()
File "/usr/lib/python2.7/site-packages/ansible/cli/playbook.py", line 154, in run
results = pbex.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 153, in run
result = self._tqm.run(play=play)
File "/usr/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 284, in run
play_return = strategy.run(iterator, play_context)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/linear.py", line 221, in run
results.extend(self._execute_meta(task, play_context, iterator, host))
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 908, in _execute_meta
connection.reset()
AttributeError: 'NoneType' object has no attribute 'reset'

# Fixed code
def reset(self):
    # If we have a persistent ssh connection (ControlPersist), we can ask it to stop listening.
    cmd = map(
        to_bytes,
        self._build_command(self._play_context.ssh_executable, "-O", "stop", self.host),
    )
    controlpersist, controlpath = self._persistence_controls(cmd)
    if controlpersist:
        display.vvv("sending stop: %s" % cmd)
        p = subprocess.Popen(
            cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        stdout, stderr = p.communicate()
        status_code = p.wait()
        if status_code != 0:
            raise AnsibleError("Cannot reset connection:\n%s" % stderr)

    self.close()