Assign the result from a powershell script to ansible playbook variable

Control Node:

  • CentOS 7

  • Ansible 2.1

Remote Node:

  • Windows 7

  • Powershell 3

I’m running a powershell script (from my playbook) that returns a number via stdout. How do I assign the result (6999) to my playbook variable named machineId?

Here’s my playbook…

I would say set_fact with machineID=out.stdout should do the trick.
But I am not sure if this does what you actually want to achieve.

Johannes

Oh, Thanks, yes that was what I needed. Actually here is what I did in my playbook…

script: machineId.ps1
register: result

  • set_fact: machineId={{result.stdout_lines}}

Hey, you might want to use

  • set_fact: machineId={{result.stdout_lines.0}}

as stdout_lines is an array rather than a string. Its hard to tell from this example because you only have one line in the example. But handy to know about as sometimes what you need is the contents of the 3rd line or whatever.

Hope this helps,

Jon

Very good point. Thanks for your help!!!