ffs
ffs

Reputation: 107

How to read the value returned by a powershell script with ansible

I am trying the following Powershell script:

Function jello-code {
    return "jello"
}

jello-code

In Ansible playbook I have a task defined as

- name : run jello script.
  win_shell: C:Projects\scripts\jello.ps1
  args:
    chdir:  C:Projects\scripts
  register : script_return
- debug : msg = {{script_return.stdout}}

Currently the value shown by debug task is

ok: [clientmachine.com] => {
    "msg": "Hello world!"
}

I always get the same "Hello world!" message.

My goal is to do other tasks based on the value returned by the Powershell script.

Upvotes: 1

Views: 7266

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39355

Well this happens because, the default message of the debug module is Hello world! you can refer to the documentation default of the parameter msg.

And because your YAML syntax is bogus, then your debug task prints its default.

A good recommendation would be to avoid mixing the YAML syntax and the older key=value one.
A working example of this would be:

- name: run jello script.
  win_shell: C:Projects\scripts\jello.ps1
  args:
    chdir:  C:Projects\scripts
  register: script_return
- debug: 
    msg: "{{ script_return.stdout }}"

If you insist on having the other syntax — named key=value — though, note that your issue is coming from the space that you put around your equal sign.

Here are the different behaviours with and without spaces:

  • - debug: msg={{script_return.stdout}}
    
    is correct and prints
    "msg": "jello"
    
  • - debug: msg = {{script_return.stdout}}
    
    is bogus and prints
    "msg": "Hello world!"
    
  • - debug: msg ={{script_return.stdout}}
    
    is bogus and prints
    "msg": "Hello world!"
    
  • - debug: msg= {{script_return.stdout}}
    
    is bogus and prints
    "msg": ""
    

So, in short, in this syntax, do not add spaces around the equal sign.

Upvotes: 4

Related Questions