Playbook for windows - Variable error

397 views
Skip to first unread message

nbmp...@gmail.com

unread,
Oct 26, 2017, 8:14:59 AM10/26/17
to Ansible Project
Hello,

I'm stuck and cannot find a solution :-/

I have some variables in host_vars folder:
links: "{{'1.1.1.2:8080,1.1.1.3:8080,1.1.1.4:8080'}}"
log: "{{'C:\\Program Files (x86)\\Test\\'}}"


My playbook looks like this:
- name: Test script
  script: "{{ role_path }}/files/test.ps1 {{ links }} {{ log }}"
  register: result
- set_fact: test="{{ result.stdout | from_json }}"

- name: Json output
  local_action: copy content="{{ vars[item] }}" dest="/home/user/ansible/test-{{ item }}-{{ ansible_date_time.date }}.json"
  with_items:
    - test

Error:
fatal: [test]: FAILED! => {
    "changed": true, 
    "failed": 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:180\r\n+ ... 1.1.1.3:8080,1.1.1.4:8080 C:\\Program Files (x86)\\test ...\r\n+                                                               ~~~\r\n+ CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFound \r\nException\r\n+ FullyQualifiedErrorId : CommandNotFoundException\r\n", 
    "stdout": "", 
    "stdout_lines": []
}

Now without the log variable the powershell script works fine. Nothing wrong with the script.

Ansible does not understand that var log is a path. I also tried removing (x86) from the string, but then the result is 'C:\Program'. Space is apparently a separator. 

Anybody knows how to write windows paths with spaces and characters? 

J Hawkesworth

unread,
Oct 26, 2017, 3:21:22 PM10/26/17
to Ansible Project
Hi,

You can construct valid windows paths with spaces if you single quote the path, like this.

app_dir: 'C:\Program Files (x86)\My App Directory'

If you need to build up a path from a variable you may well need to use double quotes and double backslashes, like this.

another_dir: "{{ app_homedir}}\\another\\sub\\directory"

Hope this helps,

Jon

J Hawkesworth

unread,
Oct 26, 2017, 3:27:36 PM10/26/17
to Ansible Project
Also, what are you trying to do? It's worth looking at the existing windows modules and seeing if they do some of what need.

Powershell has a ConvertTo-Json cmdlet that might well simplify your code, but it's worth trying to use the modules as things like idempotency and the ability to use check mode become useful once you have got a few things automated.

Hope this helps as well,

Jon

nbmp...@gmail.com

unread,
Oct 27, 2017, 2:24:52 AM10/27/17
to Ansible Project
Thanks for the replies. This is now solved like this:

host_vars
log: C:\\Program Files (x86)\\Test

roles
- name: Test script
 script: "{{ role_path }}/files/test.ps1 {{ links }} \'{{ log }}\'"
 register: result
- set_fact: test="{{ result.stdout | from_json }}"

- name: Json output
 local_action: copy content="{{ vars[item] }}" dest="/home/user/ansible/test-{{ item }}-{{ ansible_date_time.date }}.json"
 with_items:
   - test

As to the json part, yes I am already converting pscustomobjects to json via ConvertTo-Json, but ansible I had to tell ansible that I only want the powershell json result returned. Not all the ansible debug info.

J Hawkesworth

unread,
Oct 27, 2017, 5:17:14 AM10/27/17
to Ansible Project
If its all working maybe no need to change but you might want to get your script to write a log file to disk and then use the fetch module (which also works against windows) to pull the results back onto your ansible controller.

Jon
Reply all
Reply to author
Forward
0 new messages