Assign the result from a powershell script to ansible playbook variable

2,284 views
Skip to first unread message

skinnedknuckles

unread,
May 24, 2016, 2:59:51 PM5/24/16
to Ansible Project
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...

---
- name: deploy
  hosts: windows
  vars:
    machineId: none
  tasks:
  - name: get machine id
    script: machineId.ps1
    register: out
  - debug: var=out

Here's what I get when I run the play book...


[ansmgr@dhcp1-60-20 playbooks]$ ansible-playbook deploy.yml

PLAY [deploy new software version] *********************************************

TASK [setup] *******************************************************************
ok: [ADS-6999]

TASK [get machine id] **********************************************************
changed: [ADS-6999]

TASK [debug] *******************************************************************
ok: [ADS-6999] => {
    "out": {
        "changed": true,
        "rc": 0,
        "stderr": "",
        "stdout": "6999\n",
        "stdout_lines": [
            "6999"
        ]
    }
}

PLAY RECAP *********************************************************************
ADS-6999                   : ok=3    changed=1    unreachable=0    failed=0   


Johannes Kastl

unread,
May 24, 2016, 4:10:09 PM5/24/16
to ansible...@googlegroups.com
On 24.05.16 20:59 skinnedknuckles wrote:

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

> *[ansmgr@dhcp1-60-20 playbooks]$ ansible-playbook deploy.ymlPLAY [deploy
> new software version] *********************************************TASK
> [setup]
> *******************************************************************ok:
> [ADS-6999]TASK [get machine id]
> **********************************************************changed:
> [ADS-6999]TASK [debug]
> *******************************************************************ok:
> [ADS-6999] => { "out": { "changed": true, "rc": 0,
> "stderr": "", "stdout": "6999\n", "stdout_lines":
> [ "6999" ] }}PLAY RECAP
> *********************************************************************ADS-6999
> : ok=3 changed=1 unreachable=0 failed=0 *
>

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

signature.asc

skinnedknuckles

unread,
May 24, 2016, 4:29:33 PM5/24/16
to Ansible Project
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}}

J Hawkesworth

unread,
May 25, 2016, 1:25:46 AM5/25/16
to Ansible Project
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

skinnedknuckles

unread,
May 25, 2016, 4:26:24 PM5/25/16
to Ansible Project
Very good point.  Thanks for your help!!!


On Tuesday, May 24, 2016 at 1:59:51 PM UTC-5, skinnedknuckles wrote:
Reply all
Reply to author
Forward
0 new messages