# Buggy code
def cli(self, command):
    reply = command(self.module, command)
    output = reply.find(".//output")
    if not output:
        self.module.fail_json(msg="failed to retrieve facts for command %s" % command)
    return str(output.text).strip()


# 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 cli(self, command):
    reply = command(self.module, command)
    output = reply.find(".//output")
    if not output:
        self.module.fail_json(msg="failed to retrieve facts for command %s" % command)
    return to_text(output.text).strip()