# Buggy code
def _write_backup(self, host, contents):
    backup_path = self._get_working_path() + "/backup"
    if not os.path.exists(backup_path):
        os.mkdir(backup_path)
    for fn in glob.glob("%s/%s*" % (backup_path, host)):
        os.remove(fn)
    tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
    filename = "%s/%s_config.%s" % (backup_path, host, tstamp)
    open(filename, "w").write(contents)
    return filename


# Traceback (UnicodeEncodeError)
The full traceback is:
Traceback (most recent call last):
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 316, in <module>
main()
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 310, in main
run(module, result)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 235, in run
contents = get_running_config(module)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 212, in get_running_config
return NetworkConfig(indent=1, contents=contents)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 167, in __init__
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 200, in load
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 215, in parse
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 1386: ordinal not in range(128)

fatal: [iosxr01]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 316, in <module>\n    main()\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 310, in main\n    run(module, result)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 235, in run\n    contents = get_running_config(module)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 212, in get_running_config\n    return NetworkConfig(indent=1, contents=contents)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 167, in __init__\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 200, in load\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 215, in parse\nUnicodeEncodeError: 'ascii' codec can't encode character u'\\xe6' in position 1386: ordinal not in range(128)\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def _write_backup(self, host, contents):
    backup_path = self._get_working_path() + "/backup"
    if not os.path.exists(backup_path):
        os.mkdir(backup_path)
    for fn in glob.glob("%s/%s*" % (backup_path, host)):
        os.remove(fn)
    tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
    filename = "%s/%s_config.%s" % (backup_path, host, tstamp)
    open(filename, "w").write(to_bytes(contents))
    return filename