YAML Death by non-breaking spaces
Just when you are in the flow of it - something comes up that spoils your mood.
So that I don't forget and hopefully as a note for anyone else tripped up by this.
Today a long play that I have been working on for some time suddenly started to fail with the message:
Traceback (most recent call last):
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/bin/ansible-playbook", line 326, in <module>
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/bin/ansible-playbook", line 266, in main
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 313, in run
play = Play(self, play_ds, play_basedir, vault_password=self.vault_password)
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/lib/python2.7/site-packages/ansible/playbook/play.py", line 165, in __init__
self._tasks = self._load_tasks(self._ds.get('tasks', ), load_vars)
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/lib/python2.7/site-packages/ansible/playbook/play.py", line 636, in _load_tasks
data = utils.parse_yaml_from_file(include_filename, vault_password=self.vault_password)
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/lib/python2.7/site-packages/ansible/utils/__init__.py", line 786, in parse_yaml_from_file
process_yaml_error(exc, data, path, show_content)
File "/usr/local/Cellar/ansible/1.8.4_1/libexec/lib/python2.7/site-packages/ansible/utils/__init__.py", line 719, in process_yaml_error
%s""" % (path, mark.line + 1, mark.column + 1, before_probline, probline, arrow)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
This horrible error message was quite hard to debug - not because it isn't obvious as to what has happened (the last line says it all) - a stray extended character - dopey key press - has crept into one of my yaml files - but because the character was hard to track down.
It turned out to be a non-breaking space entered by mistake by option-space (a key press that normally brings up iTerm in visor mode - so not a mistake I make very often).
By turning on 'show invisibles' it still may not show up as it looks like a space. So hunting these things down can be hard.
So in my case a simple non breaking space (U+00A0) tripped me up.
But by re-tracing your steps and checking the encoding of your recently changed files (commit often!) hopefully you will spot such things before heading off on a goose chase.
Anyhow - putting it up here so I remember and hopefully to help some other poor passer by!
Here is a good resource for decoding such issues - http://rishida.net/tools/conversion/
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Somerset, BA3 4HP, United Kingdom
- Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom
- Telephone: (+44) 01273 687900
- VAT GB 748125034
- UK Company Reg: 3932829