Syntax error after ansible update (latest)

459 views
Skip to first unread message

Nico

unread,
Apr 26, 2018, 7:47:25 AM4/26/18
to Ansible Project
Hi,

My playbook is not working anymore after updating ansible to latest version. 

For example: 
- Vault file containing variable with value
vault_win_password: foo20"

- Variable with value 
location: C:\Program files (x86)\bar

Task:
- name: Test login
  script: "{{ role_path }}/files/portal.ps1 {{ dbuser }} {{ vault_win_password }}"

Error:
The full traceback is:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/ansible-2.5.1-py2.7.egg/ansible/executor/task_executor.py", line 138, in run
    res = self._execute()
  File "/usr/local/lib/python2.7/dist-packages/ansible-2.5.1-py2.7.egg/ansible/executor/task_executor.py", line 558, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/local/lib/python2.7/dist-packages/ansible-2.5.1-py2.7.egg/ansible/plugins/action/script.py", line 79, in run
    parts = [to_text(s, errors='surrogate_or_strict') for s in shlex.split(raw_params.strip())]
  File "/usr/lib/python2.7/shlex.py", line 279, in split
    return list(lex)
  File "/usr/lib/python2.7/shlex.py", line 269, in next
    token = self.get_token()
  File "/usr/lib/python2.7/shlex.py", line 96, in get_token
    raw = self.read_token()
  File "/usr/lib/python2.7/shlex.py", line 172, in read_token
    raise ValueError, "No closing quotation"
ValueError: No closing quotation

fatal: [sj-aio01]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

- name: Test location
  script: "{{ role_path }}/files/file.ps1 {{ location}}"

fatal: [sj-can02]: FAILED! => {
    "changed": true,
    "msg": "non-zero return code",
    "rc": 1,
    "stderr": "x86 : The term 'x86' is not recognized as the name of a cmdlet, function, \r\nscript file, or operable program. Check the spelling of the name, or if a path \r\nwas included, verify that the path is correct and try again.\r\nAt line:1 char:122\r\n+ ... 42148.17-152711404220483\\cannister.ps1' C:\\Program Files (x86)\\IBM\\IB ...\r\n+                                                               ~~~\r\n    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFound \r\n   Exception\r\n    + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n\r\n",
    "stdout": "",
    "stdout_lines": []
}

Before updating this worked.
- name: Test location
  script: "{{ role_path }}/files/file.ps1 \'{{ location}}\'"

Now says:
ERROR! Syntax Error while loading YAML.
  found unknown escape character "'"

How am I supposed to pass along parameters when ansible does not understand the values anymore? 


Nico

unread,
Apr 26, 2018, 7:56:17 AM4/26/18
to Ansible Project
Also tried using single quotes but get this error:

fatal: [sj-aio01]: FAILED! => {"msg": "template error while templating string: unexpected char u'\"' at 10. String: {{foo20\"}}"}

Super annoying. 

Jordan Borean

unread,
Apr 26, 2018, 4:38:42 PM4/26/18
to Ansible Project
Your issue is that one of your arguments contains a " and when shlex tries to split the arguments as a list it fails to find the end ". E.g the value of script you set is

script: "{{ role_path }}/files/portal.ps1 {{ dbuser }} {{ vault_win_password }}"

# when taking away Jinja2 this becomes

script
: /some/path/files/portal.ps1 user foo20"

When trying to turn the string to a list of arguments ([portal.ps1", "user", "foo20\""]), it fails to find the end quote for the password quote. What you need to do is use the quote filter to automatically quote each argument, e.g.

script: "{{ role_path }}/files/portal.ps1 {{ dbuser|quote }} {{ vault_win_password|quote }}"

# if you wanted to do it manually without the quote filter (please don't)

script
: "{{ role_path }}/files/portal.ps1 '{{ dbuser }}' '{{ vault_win_password }}'"

# when taking away Jinja2 this become

script
: /some/path/files/portal.ps1 "user" "foo20\""

This means each argument is quoted so shlex can split them into a list without any issues.

You second issue also comes down to quotes, when interpreting this

script: "{{ role_path }}/files/file.ps1 {{ location }}"

# this is seen by Ansible as

script
: /some/path/files/portal.ps1 C:\Program Files (x86)\bar

When running in Windows, it sees this as 3 different arguments; ["C:\Program", "Files", "(x86)\bar"] as it is not quoted. PowerShell makes this a bit more complex, the 3rd argument (x86)\bar, PowerShell see the brackets and things the value inside is a command and so tries to run it which in turn leads to the error x86 is not recognized as the name of a cmdlet, function... The fix for this is the same as your previous issue, use the quote filter to quote the values

script: "{{ role_path }}/files/file.ps1 {{ location }}"

# without using the quote filter this is how it can be done (once again please don't this is just to illustrate how it could be done)

script
: "{{ role_path }}/files/file.ps1 \"{{ location }}\""
# or
script
: "{{ role_path }}/files/file.ps1 '{{ location }}'"

# this is seen by Ansible as

script
: /some/path/files/portal.ps1 "C:\Program Files (x86)\bar"

Now because the path is quoted, it is seen as 1 argument instead of 3.

Thanks

Jordan

Nico

unread,
Apr 30, 2018, 3:23:14 AM4/30/18
to Ansible Project
Thank you very much Jordan. That helped! 

Much appreciated!!! 
Reply all
Reply to author
Forward
0 new messages