# Output of windows raw cmd as var

**URL:** <https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 1, 2015, 4:22pm UTC](https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156 "2015-09-01T16:22:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [September 1, 2015, 4:22pm UTC](https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156/1 "2015-09-01T16:22:22Z")

</div>

Hi, all.

I’ve been struggling with trying to get Ansible working for Windows automation. Perhaps I’m asking too much, but here’s what I’m after - I want to update a group of servers, stop a particular service on a few of those server, then reboot the entire group. Updating and rebooting are the easy parts. My challenge is in identifying the servers that have the particular service running, and stopping that service if it’s running. The command that I’d use at the Windows cli would be this: sc query ListManager | find “RUNNING” or  
sc query ListManager | find “STATE”, which return “STATE : 4 RUNNING”. I can extend that a bit, like so: sc query ListManager | find “RUNNING” \>nul 2\>&1 && echo running, which obviously returns “running”.

Here’s my play, so far:

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [September 1, 2015, 5:36pm UTC](https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156/2 "2015-09-01T17:36:42Z")

</div>

Hi Dimitri,

A couple of things:

First, please try out the new win\_updates module [here](https://github.com/ansible/ansible-modules-extras/pull/854) (and +1 the PR if it works for you) - the way you’re kicking off the Windows updates asynchronously can fail in all sorts of interesting ways.

Second, while the approach you’re using will work, it can also mask other failures. I’m guessing you don’t really care if the services are running or not, merely that they’re installed, and if so, they should be stopped. The win\_service module (and most Ansible modules) are designed to enforce a desired state, not just blindly run commands, so that part’s already taken care of. To just skip the task if not installed, I’d suggest something like:

tasks:

- name: check for Lyris service  
raw: sc query ListManager  
register: lyris\_hint  
failed\_when: lyris\_hint.rc not in [0,1060] # 1060 == Service not installed

- name: stop services  
service: name={{ item }} state=stopped  
with\_items: [“ListManager”, “LyrisAlert”]  
when: lyris\_hint.rc == 0 # ie, the ListManager service is installed

This way, if it fails for some other reason than the service not being installed, your playbook will break instead of masking the failure.

Good luck!

-Matt

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [September 1, 2015, 7:44pm UTC](https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156/3 "2015-09-01T19:44:17Z")

</div>

Matt,

Superb, and thanks!

I went ahead and installed the latest win\_updates module. The only reason I wasn’t using it is that I’ve had good luck with running native Windows commands via the raw command. I understand why you recommend using win\_updates, though, and I’ll work with it.

The two service-related tasks work a treat! Being pretty new to ansible, I was wondering where you got this from?: lyris\_hint.rc not in [0,1060]. Knowing such things looks to be extremely valuable.

Also, why am I not seeing the output of sc query ListManager if I add - debug: var=reboot\_hint.stdout\_lines ? I created another play that returns whether or not a Linux x system requires a reboot because the kernel was updated:

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [September 1, 2015, 8:03pm UTC](https://forum.ansible.com/t/output-of-windows-raw-cmd-as-var/19156/4 "2015-09-01T20:03:27Z")

</div>

Ah, and now I remember - I couldn’t get the win\_updates module to work. When I update Windows servers, I want the install all available updates (e.g. critical and important), with the exception of optional. I’ve created a play to look like this:
