# Buggy code
def _connect(self):
    super(Connection, self)._connect()

    display.display("ssh connection done, starting ncclient", log_only=True)

    allow_agent = True
    if self._play_context.password is not None:
        allow_agent = False
    setattr(self._play_context, "allow_agent", allow_agent)

    key_filename = None
    if self._play_context.private_key_file:
        key_filename = os.path.expanduser(self._play_context.private_key_file)

    if self._network_os == "default":
        for cls in netconf_loader.all(class_only=True):
            network_os = cls.guess_network_os(self)
            if network_os:
                display.display("discovered network_os %s" % network_os, log_only=True)
                self._network_os = network_os

    device_params = {
        "name": NETWORK_OS_DEVICE_PARAM_MAP.get(self._network_os) or self._network_os
    }

    ssh_config = os.getenv("ANSIBLE_NETCONF_SSH_CONFIG", False)
    if ssh_config in BOOLEANS_TRUE:
        ssh_config = True
    else:
        ssh_config = None

    try:
        self._manager = manager.connect(
            host=self._play_context.remote_addr,
            port=self._play_context.port or 830,
            username=self._play_context.remote_user,
            password=self._play_context.password,
            key_filename=str(key_filename),
            hostkey_verify=self.get_option("host_key_checking"),
            look_for_keys=self.get_option("look_for_keys"),
            device_params=device_params,
            allow_agent=self._play_context.allow_agent,
            timeout=self._play_context.timeout,
            ssh_config=ssh_config,
        )
    except SSHUnknownHostError as exc:
        raise AnsibleConnectionFailure(str(exc))
    except ImportError as exc:
        raise AnsibleError(
            "connection=netconf is not supported on {0}".format(self._network_os)
        )

    if not self._manager.connected:
        return 1, b"", b"not connected"

    display.display("ncclient manager object created successfully", log_only=True)

    self._connected = True

    netconf = netconf_loader.get(self._network_os, self)
    if netconf:
        display.display(
            "loaded netconf plugin for network_os %s" % self._network_os, log_only=True
        )
    else:
        netconf = netconf_loader.get("default", self)
        display.display(
            "unable to load netconf plugin for network_os %s, falling back to default plugin"
            % self._network_os
        )
    self._implementation_plugins.append(netconf)

    return 0, to_bytes(self._manager.session_id, errors="surrogate_or_strict"), b""


# Traceback (SSHError)
2018-03-09 11:53:50,215 p=23960 u=pffs |  TASK [INFO] *************************************************************************************************************************************************************************************************************************************************************
2018-03-09 11:53:50,215 p=23960 u=pffs |  task path: /home/pffs/scripts/ansible-test/testbook.yaml:15
2018-03-09 11:53:50,215 p=23960 u=pffs |  Friday 09 March 2018  11:53:50 -0500 (0:00:13.683)       0:00:14.058 **********
2018-03-09 11:53:53,478 p=20104 u=pffs |  creating new control socket for host 192.168.100.104:22 as user juniper
2018-03-09 11:53:53,478 p=20104 u=pffs |  control socket path is /home/pffs/.ansible/pc/663894f18e
2018-03-09 11:53:53,478 p=20104 u=pffs |  current working directory is /home/pffs/scripts/ansible-test
2018-03-09 11:53:53,478 p=20104 u=pffs |  using connection plugin netconf
2018-03-09 11:53:53,750 p=20104 u=pffs |  network_os is set to junos
2018-03-09 11:53:53,762 p=20104 u=pffs |  ssh connection done, stating ncclient
2018-03-09 11:54:03,779 p=20104 u=pffs |  failed to create control socket for host 192.168.100.104
2018-03-09 11:54:03,781 p=20104 u=pffs |  Traceback (most recent call last):
File "/home/pffs/scripts/ansible-test/.venv-p2/bin/ansible-connection", line 315, in main
server = Server(socket_path, pc)
File "/home/pffs/scripts/ansible-test/.venv-p2/bin/ansible-connection", line 112, in __init__
self.connection._connect()
File "/home/pffs/scripts/ansible-test/.venv-p2/lib/python2.7/site-packages/ansible/plugins/connection/netconf.py", line 158, in _connect
ssh_config=ssh_config
File "/home/pffs/scripts/ansible-test/.venv-p2/lib/python2.7/site-packages/ncclient/manager.py", line 154, in connect
return connect_ssh(*args, **kwds)
File "/home/pffs/scripts/ansible-test/.venv-p2/lib/python2.7/site-packages/ncclient/manager.py", line 119, in connect_ssh
session.connect(*args, **kwds)
File "/home/pffs/scripts/ansible-test/.venv-p2/lib/python2.7/site-packages/ncclient/transport/ssh.py", line 383, in connect
raise SSHError("Could not open socket to %s:%s" % (host, port))
SSHError: Could not open socket to 192.168.100.104:22

# Fixed code
def _connect(self):
    super(Connection, self)._connect()

    display.display("ssh connection done, starting ncclient", log_only=True)

    allow_agent = True
    if self._play_context.password is not None:
        allow_agent = False
    setattr(self._play_context, "allow_agent", allow_agent)

    key_filename = None
    if self._play_context.private_key_file:
        key_filename = os.path.expanduser(self._play_context.private_key_file)

    if self._network_os == "default":
        for cls in netconf_loader.all(class_only=True):
            network_os = cls.guess_network_os(self)
            if network_os:
                display.display("discovered network_os %s" % network_os, log_only=True)
                self._network_os = network_os

    device_params = {
        "name": NETWORK_OS_DEVICE_PARAM_MAP.get(self._network_os) or self._network_os
    }

    ssh_config = self.get_option("netconf_ssh_config")
    if ssh_config in BOOLEANS_TRUE:
        ssh_config = True
    else:
        ssh_config = None

    try:
        self._manager = manager.connect(
            host=self._play_context.remote_addr,
            port=self._play_context.port or 830,
            username=self._play_context.remote_user,
            password=self._play_context.password,
            key_filename=str(key_filename),
            hostkey_verify=self.get_option("host_key_checking"),
            look_for_keys=self.get_option("look_for_keys"),
            device_params=device_params,
            allow_agent=self._play_context.allow_agent,
            timeout=self._play_context.timeout,
            ssh_config=ssh_config,
        )
    except SSHUnknownHostError as exc:
        raise AnsibleConnectionFailure(str(exc))
    except ImportError as exc:
        raise AnsibleError(
            "connection=netconf is not supported on {0}".format(self._network_os)
        )

    if not self._manager.connected:
        return 1, b"", b"not connected"

    display.display("ncclient manager object created successfully", log_only=True)

    self._connected = True

    netconf = netconf_loader.get(self._network_os, self)
    if netconf:
        display.display(
            "loaded netconf plugin for network_os %s" % self._network_os, log_only=True
        )
    else:
        netconf = netconf_loader.get("default", self)
        display.display(
            "unable to load netconf plugin for network_os %s, falling back to default plugin"
            % self._network_os
        )
    self._implementation_plugins.append(netconf)

    return 0, to_bytes(self._manager.session_id, errors="surrogate_or_strict"), b""