Repository: ansible/ansible

Issue:
ascii' codec can't encode character 
##### ISSUE TYPE
 - Bug Report
##### COMPONENT NAME
command & fail

##### ANSIBLE VERSION
ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, May  3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]

##### CONFIGURATION
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = [u'/etc/ansible/hosts']
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
PERSISTENT_CONNECT_TIMEOUT(/etc/ansible/ansible.cfg) = 30

##### OS / ENVIRONMENT
<!---
Red Hat Enterprise Linux 7.4

##### SUMMARY
Trying to run below playbook but it's getting fail of error ascii code.

---
- hosts: test
  tasks:

# Checking application status #
  - name: Checking application status
    command: /usr/bin/systemctl status rsyslog
    register: application_status

# Abort patching if application is running #
  - name: Abort patching if application is running
    fail:  msg="have running Application.Please stop the application first, then attempt patching.”
    when: application_status.stdout == "inactive"

  - name: create testfile
    command: touch /home/test/servicecheck.txt


##### STEPS TO REPRODUCE
[root@test1$]ansible-playbook -vvv -i inventory servicecheck.yml --check
ansible-playbook 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, May  3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]
Using /etc/ansible/ansible.cfg as config file
Parsed /home/test/playbooks/patching/inventory inventory source with ini plugin
ERROR! Unexpected Exception, this is probably a bug: 'ascii' codec can't encode character u'\u201d' in position 87: ordinal not in range(128)
the full traceback was:

Traceback (most recent call last):
  File "/bin/ansible-playbook", line 106, in <module>
    exit_code = cli.run()
  File "/usr/lib/python2.7/site-packages/ansible/cli/playbook.py", line 130, in run
    results = pbex.run()
  File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in run
    pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
    pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 106, in _load_playbook_data
    entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 117, in load
    return p.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 254, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 147, in _load_tasks
    return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/helpers.py", line 59, in load_list_of_blocks
    loader=loader,
  File "/usr/lib/python2.7/site-packages/ansible/playbook/block.py", line 86, in load
    return b.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 254, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/lib/python2.7/site-packages/ansible/playbook/block.py", line 122, in _load_block
    use_handlers=self._use_handlers,
  File "/usr/lib/python2.7/site-packages/ansible/playbook/helpers.py", line 342, in load_list_of_tasks
    t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 138, in load
    return t.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 244, in load_data
    ds = self.preprocess_data(ds)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 178, in preprocess_data
    (action, args, delegate_to) = args_parser.parse()
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 295, in parse
    action, args = self._normalize_parameters(thing, action=action, additional_args=additional_args)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 159, in _normalize_parameters
    args = self._normalize_new_style_args(thing, action)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 204, in _normalize_new_style_args
    args = parse_kv(thing, check_raw=check_raw)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/splitter.py", line 62, in parse_kv
    vargs = split_args(args)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/splitter.py", line 264, in split_args
    raise AnsibleParserError("failed at splitting arguments, either an unbalanced jinja2 block or quotes: {}".format(args))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201d' in position 87: ordinal not in range(128)


Discussion:
##### OS / ENVIRONMENT
Red Hat Enterprise Linux v.7.4

##### SUMMARY
We are trying to execute playbook but its giving error.

```yaml
---
- hosts: test
  tasks:

# Checking application status #
  - name: Checking application status
    command: /usr/bin/systemctl status rsyslog
    register: application_status

# Abort patching if application is running #
  - name: Abort patching if application is running
    fail:  msg="have running Application.Please stop the application first, then attempt patching.”
    when: application_status.stdout == "inactive"

  - name: create testfile
    command: touch /home/test/servicecheck.txt
```

##### STEPS TO REPRODUCE
```
ansible-playbook 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, May  3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]
Using /etc/ansible/ansible.cfg as config file
Parsed /home/test/playbooks/patching/inventory inventory source with ini plugin
ERROR! Unexpected Exception, this is probably a bug: 'ascii' codec can't encode character u'\u201d' in position 87: ordinal not in range(128)
the full traceback was:

Traceback (most recent call last):
  File "/bin/ansible-playbook", line 106, in <module>
    exit_code = cli.run()
  File "/usr/lib/python2.7/site-packages/ansible/cli/playbook.py", line 130, in run
    results = pbex.run()
  File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in run
    pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
    pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 106, in _load_playbook_data
    entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 117, in load
    return p.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 254, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/lib/python2.7/site-packages/ansible/playbook/play.py", line 147, in _load_tasks
    return load_list_of_blocks(ds=ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/helpers.py", line 59, in load_list_of_blocks
    loader=loader,
  File "/usr/lib/python2.7/site-packages/ansible/playbook/block.py", line 86, in load
    return b.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 254, in load_data
    self._attributes[name] = method(name, ds[name])
  File "/usr/lib/python2.7/site-packages/ansible/playbook/block.py", line 122, in _load_block
    use_handlers=self._use_handlers,
  File "/usr/lib/python2.7/site-packages/ansible/playbook/helpers.py", line 342, in load_list_of_tasks
    t = Task.load(task_ds, block=block, role=role, task_include=task_include, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 138, in load
    return t.load_data(data, variable_manager=variable_manager, loader=loader)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 244, in load_data
    ds = self.preprocess_data(ds)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 178, in preprocess_data
    (action, args, delegate_to) = args_parser.parse()
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 295, in parse
    action, args = self._normalize_parameters(thing, action=action, additional_args=additional_args)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 159, in _normalize_parameters
    args = self._normalize_new_style_args(thing, action)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/mod_args.py", line 204, in _normalize_new_style_args
    args = parse_kv(thing, check_raw=check_raw)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/splitter.py", line 62, in parse_kv
    vargs = split_args(args)
  File "/usr/lib/python2.7/site-packages/ansible/parsing/splitter.py", line 264, in split_args
    raise AnsibleParserError("failed at splitting arguments, either an unbalanced jinja2 block or quotes: {}".format(args))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201d' in position 87: ordinal not in range(128)
```
cc @dagwieers
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@pratikkava1189 Thanks for reporting this issue. I think you have non-ascii character in your PB (”) at line `Please stop the application first, then attempt patching.`. Re-try after removing it. I agree that stack trace should be handled.
@gundalow What do you recommend on this bug - should we close this as traceback is only shown by `-vvv` parameter?
@Akasurde @gundalow , Ideally there should not be restriction of quote in playbook, it make more complex for engineer to identify the issue.

However there is also one bug which currently I'm facing issue related to whitespace in playbook, I will open new issue for that bug, thanks.
Not sure, added to Core agenda
Smart quotes aren't quotes in most computer-related things (config files, programming languages, the yaml specification, etc).  So this definitely should be an error.  However, you should not get a unicode traceback from it.  Instead we should tell you that there was a problem in splitting the string during key=value parsing.  On python3, it gives the right error message:
```
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: msg="have running Application.Please stop the application first, then attempt patching.”

The error appears to have been in '/srv/ansible/abadger6/lib/ansible/parsing/test.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - fail:  msg="have running Application.Please stop the application first, then attempt patching.”
    ^ here
```

I'll fix the code so that Python2 gives that error as well.

Resolving patch:
diff --git a/lib/ansible/parsing/splitter.py b/lib/ansible/parsing/splitter.py
--- a/lib/ansible/parsing/splitter.py
+++ b/lib/ansible/parsing/splitter.py
@@ -261,6 +261,6 @@ def split_args(args):
     # If we're done and things are not at zero depth or we're still inside quotes,
     # raise an error to indicate that the args were unbalanced
     if print_depth or block_depth or comment_depth or inside_quotes:
-        raise AnsibleParserError("failed at splitting arguments, either an unbalanced jinja2 block or quotes: {}".format(args))
+        raise AnsibleParserError(u"failed at splitting arguments, either an unbalanced jinja2 block or quotes: {0}".format(args))
 
     return params


Test patch: