# UnicodeEncodeError

**URL:** <https://forum.ansible.com/t/unicodeencodeerror/19159>\
**Category:** Ansible Project\
**Created:** [September 1, 2015, 7:58pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159 "2015-09-01T19:58:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [September 1, 2015, 7:58pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/1 "2015-09-01T19:58:34Z")

</div>

Hi all,

Trying to run a playbook here, but getting a Unicode error. Here’s what I’m doing:

The plays:

**main.yml:**

- name: Check if new upstart job is needed  
template:  
src=syslog-ng-upstart.conf.j2  
dest=/etc/init/syslog-ng.conf  
owner=root  
group=root  
mode=0644  
backup=yes  
register: upstart

- name: Update upstart configuration  
include: upstart.yml  
when: upstart.changed​​

---

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [September 1, 2015, 8:03pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/2 "2015-09-01T20:03:31Z")

</div>

Also, I tried changing “module\_lang” to “en\_US.UTF-8” in /etc/ansible/ansible.cfg, but it made no difference. I don’t have a ansible.cfg in the playbook directory or .ansible.cfg in the home directory.

Ansible version is 1.7.2.

Thanks!

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [September 2, 2015, 7:36pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/3 "2015-09-02T19:36:42Z")

</div>

There have been sporadic UnicodeErrors that crop up from time to time.  
I've been trying squash them as they come up. 1.7.2 is pretty old so  
it won't have a lot of those fixes. Are you able to test (at least  
the failing task) on a newer version of ansible? That would tell you  
if the problem has been fixed already in a newer version.

If it's still failing with the newer version, narrowing down the  
problem would be helpful. Is it occurring with the first task or the  
second? (I'm guessing the first). What is the output from  
ansible-playbook -vvvv ? What LC\* and LANG settings do you have on the  
remote box?

-Toshio

---

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [September 2, 2015, 10:41pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/4 "2015-09-02T22:41:22Z")

</div>

Hey Toshio! Thanks for your reply.

Since these are production machines, I can only use 1.7.2 at the moment. However, I installed the newest ppa version (1.9.2) on a VM cluster to test.  
The error stills pops up. The error happens when it tries running the first task from the second playbook. However, commenting that task out just makes the error pop up in the next task. So I believe it’s already carrying it from the first playbook, after completing the “Check if new upstart job is needed” task.

The LC and LANG settings are “en\_US.UTF-8” in the remote box.

-vvvv doesn’t give me much new info. Here’s the compressed output when the error occurs:

**ok: [anakin] =\> {“changed”: false, “gid”: 0, “group”: “root”, “mode”: “0644”, “owner”: “root”, “path”: “/etc/init/syslog-ng.conf”, “size”: 615, “state”: “file”, “uid”: 0}**

**TASK: [syslog-client | debug var=upstart] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\***  
 **ESTABLISH CONNECTION FOR USER: \*\*\*\*\*\***  
**ok: [anakin] =\> {**  
**“var”: {**  
**“upstart\_times”: {**  
**“changed”: false,**  
**“gid”: 0,**  
**“group”: “root”,**  
**“invocation”: {**  
**“module\_args”: “src=syslog-ng-upstart.conf.j2 dest=/etc/init/syslog-ng.conf owner=root group=root mode=0644 backup=yes”,**  
**“module\_name”: “template”**  
**},**  
**“mode”: “0644”,**  
**“owner”: “root”,**  
**“path”: “/etc/init/syslog-ng.conf”,**  
**“size”: 615,**  
**“state”: “file”,**  
**“uid”: 0**  
**}**  
**}**  
**}**

**TASK: [syslog-client | Stop the syslog-ng service before upgrading to upstart] \*\*\***  
**Traceback (most recent call last):**  
**File “/usr/bin/ansible-playbook”, line 324, in**   
**sys.exit(main(sys.argv[1:]))**  
**File “/usr/bin/ansible-playbook”, line 264, in main**  
**pb.run()**  
**File “/usr/lib/pymodules/python2.7/ansible/playbook/init.py”, line 348, in run**  
**if not self.\_run\_play(play):**  
**File “/usr/lib/pymodules/python2.7/ansible/playbook/init.py”, line 789, in \_run\_play**  
**if not self.\_run\_task(play, task, False):**  
**File “/usr/lib/pymodules/python2.7/ansible/playbook/init.py”, line 497, in \_run\_task**  
**results = self.\_run\_task\_internal(task, include\_failed=include\_failed)**  
**File “/usr/lib/pymodules/python2.7/ansible/playbook/init.py”, line 439, in \_run\_task\_internal**  
**results = runner.run()**  
**File “/usr/lib/pymodules/python2.7/ansible/runner/init.py”, line 1493, in run**  
**results = [self.\_executor(h, None) for h in hosts]**  
**File “/usr/lib/pymodules/python2.7/ansible/runner/init.py”, line 590, in \_executor**  
**msg = str(ae)**  
**UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 40-41: ordinal not in range(128)**

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [September 3, 2015, 5:14am UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/5 "2015-09-03T05:14:44Z")

</div>

Not a lot to go on... I do see that the two errors are happening in  
different places. That might be bad news for you as may mean that any  
fix for 1.9.3 might not be the fix needed for 1.7.2. But let's see  
what we can find:

> TASK: [syslog-client | Stop the syslog-ng service before upgrading to  
> upstart] \*\*\*  
> Traceback (most recent call last):  
> &nbsp;&nbsp;File "/usr/bin/ansible-playbook", line 324, in \<module\>  
> &nbsp;&nbsp;&nbsp;&nbsp;sys.exit(main(sys.argv[1:]))  
> &nbsp;&nbsp;File "/usr/bin/ansible-playbook", line 264, in main  
> &nbsp;&nbsp;&nbsp;&nbsp;pb.run()  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/playbook/\_\_init\_\_.py", line  
> 348, in run  
> &nbsp;&nbsp;&nbsp;&nbsp;if not self.\_run\_play(play):  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/playbook/\_\_init\_\_.py", line  
> 789, in \_run\_play  
> &nbsp;&nbsp;&nbsp;&nbsp;if not self.\_run\_task(play, task, False):  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/playbook/\_\_init\_\_.py", line  
> 497, in \_run\_task  
> &nbsp;&nbsp;&nbsp;&nbsp;results = self.\_run\_task\_internal(task, include\_failed=include\_failed)  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/playbook/\_\_init\_\_.py", line  
> 439, in \_run\_task\_internal  
> &nbsp;&nbsp;&nbsp;&nbsp;results = runner.run()  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/runner/\_\_init\_\_.py", line 1493,  
> in run  
> &nbsp;&nbsp;&nbsp;&nbsp;results = [self.\_executor(h, None) for h in hosts]  
> &nbsp;&nbsp;File "/usr/lib/pymodules/python2.7/ansible/runner/\_\_init\_\_.py", line 590,  
> in \_executor  
> &nbsp;&nbsp;&nbsp;&nbsp;msg = str(ae)  
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 40-41:  
> ordinal not in range(128)

This is an error when trying to format this exception into a string  
for display. We can probably just substitute that line like this:

- msg = str(ae)  
+ from ansible.utils.unicode import to\_bytes  
+ msg = to\_bytes(ae)

That should solve this traceback but the fact we're getting an  
exception there means that something else has gone wrong... It could  
be something that you're expected to be able to handle by changing  
configuration in your network though, so doing this so that you can  
see what the actual error is should be helpful.

If we're lucky, once you see what the underlying error is here, you  
can correct something else on your network and then 1.7.2 will run for  
you as well.

I'll add a patch to the stable-1.9 tree to do this as well. Not sure  
if it will make 1.9.3 or not, though.

[https://github.com/ansible/ansible/commit/f80494e434d36c1845c9fb830b6bd26ebcf35a89](https://github.com/ansible/ansible/commit/f80494e434d36c1845c9fb830b6bd26ebcf35a89)

> > \>\> fatal: [\*\*\*\*\*\*] =\> Traceback (most recent call last):  
> > \>\> File "/usr/lib/python2.7/dist-packages/ansible/runner/\_\_init\_\_.py",  
> > \>\> line  
> > \>\> 561, in \_executor  
> > \>\> exec\_rc = self.\_executor\_internal(host, new\_stdin)  
> > \>\> File "/usr/lib/python2.7/dist-packages/ansible/runner/\_\_init\_\_.py",  
> > \>\> line  
> > \>\> 666, in \_executor\_internal  
> > \>\> return self.\_executor\_internal\_inner(host, self.module\_name,  
> > \>\> self.module\_args, inject, port, complex\_args=complex\_args)  
> > \>\> File "/usr/lib/python2.7/dist-packages/ansible/runner/\_\_init\_\_.py",  
> > \>\> line  
> > \>\> 756, in \_executor\_internal\_inner  
> > \>\> if not utils.check\_conditional(cond, self.basedir, inject,  
> > \>\> fail\_on\_undefined=self.error\_on\_undefined\_vars):  
> > \>\> File "/usr/lib/python2.7/dist-packages/ansible/utils/\_\_init\_\_.py",  
> > \>\> line  
> > \>\> 255, in check\_conditional  
> > \>\>  
> > \>\> UnicodeEncodeError: 'ascii' codec can't encode characters in position  
> > \>\> 15-16: ordinal not in range(128)

1.7.2 is old and I'm not sure of some of the ramifications of making  
changes in the code (in newer versions of ansible we've been moving  
towards making sure that internally we handle everything as unicode  
strings and only using bytes when sending things externally. In 1.7.2  
we weren't doing that so most things were likely byte strings. jinja2  
takes and returns unicode type, however, so that's likely why we have  
a problem converting from what jinja2 is returning to us here.) but we  
may be able to fix this with the following code change:

- original = str(conditional).replace("jinja2\_compare ","")  
+ tmp\_conditional = to\_bytes(conditional,  
errors='strict').replace("jinja2\_compare ","")  
+ original = tmp\_conditional.replace("jinja2\_compare ","")

Not sure if that will just bring us another error, further down the  
line, though.

-Toshio

---

<div class="post-metadata">

**Author:** ![Eugene\_Romero](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Eugene\_Romero](https://forum.ansible.com/u/Eugene_Romero)\
**Post date:** [September 14, 2015, 5:00pm UTC](https://forum.ansible.com/t/unicodeencodeerror/19159/6 "2015-09-14T17:00:43Z")

</div>

Thanks again for your reply Toshio. I apologize for not getting back to you sooner, was off of work for a week and a half. While I was gone, one of my fellow sysadmins updated ansible to 1.9.3, so I am now able to test the failing playbook in production. Once I get up to speed with everything and have a chance to check, I’ll post back here if I’m still having issues. Let’s hope 1.9.3 fixed the problem 🙂

Gene
