# Parsing for win\_updates reboot hint

**URL:** <https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 30, 2015, 1:38pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387 "2015-09-30T13:38:33Z")\
**Posts on this page:** 9\
**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 30, 2015, 1:38pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/1 "2015-09-30T13:38:33Z")

</div>

Hello, all.

With help from the list, I have windows updates working via the rewritten win\_updates module found [here](https://github.com/ansible/ansible-modules-extras/pull/854). I’m now looking for a way to creat a reboot hint based on the return output. Here’s what that output partially looks like when I run the module (using -v):

changed: [sawintest02] =\> {“changed”: true, “failed\_update\_count”: 0, “found\_update\_count”: 23, “installed\_update\_count”: 23, “reboot\_required”: true, “updates”: {“04f04253-f803-43b8-8b37-6841c614dd9f”: {“id”: “04f04253-f803-43b8-8b37-6841c614dd9f”, “installed”: true, “kb”: [“3084135”], “title”: “Security Update for Windows Server 2012 R2 (KB3084135)”}, ~

How would I parse this output to create a hint so that a reboot will be done if “reboot\_required”: true, and nothing be done if “reboot\_required”: false,

I’m a terrible programmer, so your help is greatly appreciated.

Dimitri

---

<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:** [September 30, 2015, 3:06pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/2 "2015-09-30T15:06:25Z")

</div>

You need to use ‘register’ to capture the results into a variable: [http://docs.ansible.com/ansible/playbooks\_variables.html#registered-variables](http://docs.ansible.com/ansible/playbooks_variables.html#registered-variables)

Then you can call another module to do the reboot followed by a ‘when’, probably something like

when: result\_var.reboot\_required

worth having a look at Trond Hindenes’ win\_reboot role in galaxy too: [https://galaxy.ansible.com/list#/roles/4576](https://galaxy.ansible.com/list#/roles/4576)

---

<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 30, 2015, 3:41pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/3 "2015-09-30T15:41:54Z")

</div>

Thanks, Jon.

This should work?:

- name: win update  
win\_updates:  
category: [‘SecurityUpdates’,‘CriticalUpdates’,‘Updates’,‘Tools’,‘DefinitionUpdates’,‘UpdateRollups’]  
register: reboot\_hint

- name: reboot server  
raw: ‘cmd /c shutdown /r /t 0’  
when: reboot\_hint.stdout.find(‘“reboot\_required”: true’) != -1

I’ll also be sure to take a look at Trond’s win\_reboot role in galaxy.

---

<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 30, 2015, 5:53pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/4 "2015-09-30T17:53:34Z")

</div>

The above doesn’t work. If I run as-is, then I get the following error:

when: reboot\_hint.stdout.find(‘“reboot\_required”: true’) != -1  
^  
This one looks easy to fix. There seems to be an extra unquoted colon in the line  
and this is confusing the parser. It was only expecting to find one free  
colon. The solution is just add some quotes around the colon, or quote the  
entire line after the first colon.

I have to parse for “reboot\_required”: true, since if no reboot is required, output is “reboot\_required”: false. Neither quoting the second colon nor the entire line after the first colon work. How do I correct for this?

---

<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:** [September 30, 2015, 7:01pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/5 "2015-09-30T19:01:54Z")

</div>

I suggest you add a debug on the registered variable and/or run ansible-playbook with -v  
this will show you the contents of the registered variable. It will be some JSON. Sometimes its useful to paste the JSON into [http://jsonlint.com/](http://jsonlint.com/) so you can see a pretty printed version of the JSON.

then it should be fairly straightforward to work out what you need in the when:

I _think_

when: reboot\_hint.results.reboot\_required

might be enough or possibly

when: reboot\_hint.results.reboot\_required == true

Hope this helps.

Jon

---

<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 30, 2015, 7:49pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/6 "2015-09-30T19:49:10Z")

</div>

Jon,

Both of your suggestions work; thanks!

For my edification, why do either work when “reboot\_required”: true, and not “reboot\_required”: false, or why don’t they simply resolve against “reboot\_required” (without true or false)?

Dimitri

---

<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:** [October 1, 2015, 5:35am UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/7 "2015-10-01T05:35:19Z")

</div>

Breaking it down a bit

reboot\_hint

is the name of the variable you have chosen to store the registered output in.

Inside reboot\_hint is the output from the module. The module results are put into a key value structure with a key of results. This is probably done to keep the structured module output separate from other things that might come out of the module (like stdout and stderr contents). Within results there are more key-and-value pairs, and the one we are interested in is reboot\_required.

So that gets us to

reboot\_hint.results.reboot\_required

For when: to be useful it need to receive the some kind of test result - something that is either true or false. We are in luck here because  
reboot\_required contains a value of false or true so when: can use that directly.

To make a slightly silly example, if reboot\_required returned a string like “next\_wednesday” then you would have to write the when: like this

when: reboot\_hint.results.reboot\_required == “next\_wednesday”

There’s a lot more you can do with this. Two things that are well worth understanding are the kinds of things you can put in module output - well worth a read of the JSON spec which is only 1 page and can be found here: [www.json.org](http://www.json.org) and the other is getting an idea of the things you can do with variables using filters in Ansible see [http://docs.ansible.com/ansible/playbooks\_filters.html](http://docs.ansible.com/ansible/playbooks_filters.html) for ansible’s built in filters and here for the full list of stuff that jinja2 (which ansible uses) can do. [http://jinja.pocoo.org/docs/templates/#builtin-filters](http://jinja.pocoo.org/docs/templates/#builtin-filters)

Hope this helps,

Jon

---

<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:** [October 1, 2015, 2:14pm UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/8 "2015-10-01T14:14:21Z")

</div>

Thank you so much for the explanation. I understand it, but am curious as to why “reboot required”, without == true or == false, will reboot the server if true is, in fact, returned, and not reboot if false is returned. Does “when” default to true if no argument is explicitly provided?

---

<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:** [October 2, 2015, 8:56am UTC](https://forum.ansible.com/t/parsing-for-win-updates-reboot-hint/19387/9 "2015-10-02T08:56:35Z")

</div>

the when: doesn’t really default to true. Instead when: just needs to ‘see’ a true or a false. It doesn’t care if that is from finding a value of true or false by looking up what is stored in a variable or by running something that returns a true or false.

adding the ‘== true’ makes the when: test that reboot\_required is set to true. Without it, the when: just sees the contents of the reboot\_required variable (which happens to be either true or false).

Hope that helps

Jon
