# Assign the result from a powershell script to ansible playbook variable

**URL:** <https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 24, 2016, 6:59pm UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556 "2016-05-24T18:59:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 24, 2016, 6:59pm UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556/1 "2016-05-24T18:59:51Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 24, 2016, 8:09pm UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556/2 "2016-05-24T20:09:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 24, 2016, 8:29pm UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556/3 "2016-05-24T20:29:33Z")

</div>

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}}

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 25, 2016, 5:25am UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556/4 "2016-05-25T05:25:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 25, 2016, 8:26pm UTC](https://forum.ansible.com/t/assign-the-result-from-a-powershell-script-to-ansible-playbook-variable/21556/5 "2016-05-25T20:26:24Z")

</div>

Very good point. Thanks for your help!!!
