# Buggy code
def _connect(self, module):
    host = get_param(module, "host")

    kwargs = {
        "port": get_param(module, "port") or 830,
        "user": get_param(module, "username"),
    }

    if get_param(module, "password"):
        kwargs["passwd"] = get_param(module, "password")

    if get_param(module, "ssh_keyfile"):
        kwargs["ssh_private_key_file"] = get_param(module, "ssh_keyfile")

    kwargs["gather_facts"] = False
    try:
        device = Device(host, **kwargs)
        device.open()
        device.timeout = get_param(module, "timeout") or 10
    except ConnectError as exc:
        module.fail_json("unable to connect to %s: %s" % (host, to_native(exc)))

    return device


# Traceback (nUnicodeEncodeError)
PLAY [Playbook Test] **************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
[WARNING]: Ignoring timeout(10) for junos_facts

fatal: [lavender.ultralab.juniper.net]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"junos_facts": {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "exception": "Traceback (most recent call last):\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible.modules.network.junos.junos_facts', init_globals=None, run_name='__main__', alter_sys=False)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 180, in run_module\n    fname, loader, pkg_name)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 72, in _run_code\n    exec code in run_globals\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/modules/network/junos/junos_facts.py\", line 135, in <module>\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/modules/network/junos/junos_facts.py\", line 126, in main\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/facts.py\", line 78, in get_facts\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/common/facts/facts.py\", line 124, in get_network_legacy_facts\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/legacy/base.py\", line 97, in populate\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/legacy/base.py\", line 54, in get_text\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 450034-450041: ordinal not in range(128)\n", "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/matucker/.ansible/tmp/ansible-local-77560craccig5/ansible-tmp-1569878043.2963982-176353774268055/AnsiballZ_junos_facts.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible.modules.network.junos.junos_facts', init_globals=None, run_name='__main__', alter_sys=False)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 180, in run_module\n    fname, loader, pkg_name)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 72, in _run_code\n    exec code in run_globals\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/modules/network/junos/junos_facts.py\", line 135, in <module>\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/modules/network/junos/junos_facts.py\", line 126, in main\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/facts.py\", line 78, in get_facts\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/common/facts/facts.py\", line 124, in get_network_legacy_facts\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/legacy/base.py\", line 97, in populate\n  File \"/var/folders/04/g24gg21176zflhxqzl_82y9c0000gn/T/ansible_junos_facts_payload_LFmG2F/ansible_junos_facts_payload.zip/ansible/module_utils/network/junos/facts/legacy/base.py\", line 54, in get_text\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 450034-450041: ordinal not in range(128)\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1, "warnings": ["Platform darwin on host lavender.ultralab.juniper.net is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information."]}}, "msg": "The following modules failed to execute: junos_facts\n"}

PLAY RECAP ************************************************************************************************************************************************************
lavender.ultralab.juniper.net : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

# Fixed code
def _connect(self, module):
    host = get_param(module, "host")

    kwargs = {
        "port": get_param(module, "port") or 830,
        "user": get_param(module, "username"),
    }

    if get_param(module, "password"):
        kwargs["passwd"] = get_param(module, "password")

    if get_param(module, "ssh_keyfile"):
        kwargs["ssh_private_key_file"] = get_param(module, "ssh_keyfile")

    kwargs["gather_facts"] = False
    try:
        device = Device(host, **kwargs)
        device.open()
        device.timeout = get_param(module, "timeout") or 10
    except ConnectError as exc:
        module.fail_json("unable to connect to %s: %s" % (host, to_text(exc)))

    return device