ansible win_updates output

634 views
Skip to first unread message

area0

unread,
Nov 30, 2017, 6:49:27 PM11/30/17
to Ansible Project
Hi guys,

I'm trying to return the following message; i basically want to display the following message and only list the 'title' field for the available update. The message should read;

" host1 has 12 updates available. The following updates are available for install:
 - KB11222
 - KB112232
  etc
"

I've tried a bunch of permutations, with the latest shown below (which doesn't work by the way).

---
- hosts: all
  gather_facts: no
  vars:
    win_update_categories:
        - CriticalUpdates
        - DefinitionUpdates
        - SecurityUpdates
        - Updates
        - UpdateRollups
  tasks:
    - block:
        - name: Checking for Available Windows Updates
          win_updates:
            category_names: "{{ win_update_categories }}"
            state: searched
          register: available_updates
        - name: List missing Updates
          debug: 
            msg: "{{ inventory_hostname }} has {{ available_updates.found_update_count }} updates available. The following updates are available for install: {{ available_updates.contains.item }}"
            with_items: "{{ title }}"

Anyone know where i'm going wrong?

Thanks in advance

area0

unread,
Nov 30, 2017, 9:01:24 PM11/30/17
to Ansible Project
Here's the raw output; each item in the 'updates' list is defined by the value of the 'id' field. Normally this would be a key like 'item' where i could then specify, updates.item.title, right?

How can i get around this?

Cheers

ok: [host1] => {
    "available_updates": {
        "changed": true,
        "failed": false,
        "found_update_count": 2,
        "installed_update_count": 0,
        "reboot_required": false,
        "updates": {
            "749e3c7a-4779-4c14-bccd-2b608c39ddc9": {
                "id": "749e3c7a-4779-4c14-bccd-2b608c39ddc9",
                "installed": false,
                "kb": [
                    "3084905"
                ],
                "title": "Update for Windows Server 2012 R2 (KB3084905)"
            },
            "8832eabb-68ee-4ef9-b45e-b8fee94581b8": {
                "id": "8832eabb-68ee-4ef9-b45e-b8fee94581b8",
                "installed": false,
                "kb": [
                    "890830"
                ],
                "title": "Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830)"
            },
},

Kai Stian Olstad

unread,
Dec 1, 2017, 11:03:51 AM12/1/17
to ansible...@googlegroups.com
On Friday, 1 December 2017 03.01.23 CET area0 wrote:
> Here's the raw output; each item in the 'updates' list is defined by the
> value of the 'id' field. Normally this would be a key like 'item' where i
> could then specify, updates.item.title, right?

I'm not sure what you mean, but it sound wrong.
Something like this will give you what you're looking for.

- debug:
msg: |
{{ inventory_hostname }} has {{ available_updates.found_update_count }} updates available. The following updates are available for install:
{% for key, value in available_updates.updates.iteritems() %}
- {{ value.kb | join(',') }}
{% endfor %}


--
Kai Stian Olstad

area0

unread,
Dec 2, 2017, 8:46:07 PM12/2/17
to Ansible Project
Perfect, thank you sir!

Much appreciated

area0

unread,
Dec 3, 2017, 5:49:51 AM12/3/17
to Ansible Project
Hi Kai,

Any way we can format the output of the debug message as below i.e. have a KB per line?

" host1 has 12 updates available. The following updates are available for install:
 - KB11222
 - KB112232
  etc"

Thank you

On Saturday, December 2, 2017 at 3:03:51 AM UTC+11, Kai Stian Olstad wrote:

Kai Stian Olstad

unread,
Dec 3, 2017, 8:56:28 AM12/3/17
to ansible...@googlegroups.com
On 03.12.2017 11:49, area0 wrote:
> Any way we can format the output of the debug message as below i.e.
> have a
> KB per line?
>
> " host1 has 12 updates available. The following updates are available
> for
> install:
> - KB11222
> - KB112232
> etc"

It should based on your output of the available_updates.
What are you seen and do you have the output of the variable that does
not give you that output?

If the "kb": field contains more than one element, my code will put them
in a comma separated sting.
If that is what you see, and just want all to be in one list this should
fix it.

- debug:
msg: |
{{ inventory_hostname }} has {{
available_updates.found_update_count }} updates available. The following
updates are available for install:
{% for key, value in available_updates.updates.iteritems() %}
{% for i in value.kb %}
- {{ i }}
{% endfor %}

area0

unread,
Dec 3, 2017, 5:49:08 PM12/3/17
to Ansible Project
Thanks Kai. The output is working fine, it's more about the formatting. For example, with the following debug code, i receive the following;

        - debug:
             msg: "{{ inventory_hostname }} has {{ available_updates.found_update_count }} updates available. 
                  {% for key, value in available_updates.updates.iteritems() %}
                - KB: {{ value.title }}
                  {% endfor %}"

ok: [host1] => {
    "msg": "host1 has 6 updates available.  - KB: Update for Windows Server 2012 R2 (KB3013769)  - KB: Update for Windows Server 2012 R2 (KB3102429)  - KB: Update for Windows Server 2012 R2 (KB3013816)  - KB: Update for Windows Server 2012 R2 (KB3084905)  - KB: 2017-11 Preview of Monthly Quality Rollup for Windows Server 2012 R2 for x64-based Systems (KB4050946)  - KB: Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830) "
}

What i'd like, is for the output to be formatted as below;

ok: [host1] => {
    "msg": "host1 has 6 updates available.  
     - KB: Update for Windows Server 2012 R2 (KB3013769)  
     - KB: Update for Windows Server 2012 R2 (KB3102429)  
     - KB: Update for Windows Server 2012 R2 (KB3013816)  
     - KB: Update for Windows Server 2012 R2 (KB3084905)  
     - KB: 2017-11 Preview of Monthly Quality Rollup for Windows Server 2012 R2 for x64-based Systems (KB4050946)  
     - KB: Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830) "
}

Is this possible?

Thank you

Kai Stian Olstad

unread,
Dec 3, 2017, 6:25:58 PM12/3/17
to ansible...@googlegroups.com
On 03.12.2017 23:49, area0 wrote:
> Thanks Kai. The output is working fine, it's more about the formatting.
> For
> example, with the following debug code, i receive the following;

Ansible is not a reporting tool so it has limit supported for nice
output on the screen.
You can put it in a file with the template module.


> - debug:
> msg: "{{ inventory_hostname }} has {{
> available_updates.found_update_count }} updates available.
> {% for key, value in
> available_updates.updates.iteritems() %}
> - KB: {{ value.title }}
> {% endfor %}"

The first ting to do is losing the quotes and add a pipe, the pipe is
essential.
To get an other output than the default one, you'll need to change the
stdout callback plugin[1].

If you use the debug, it output nice human readable text that you are
looking for, but it will be more verbose.
You could check out and test the other to see if them give you the
output you want or you could write your own.


[1]
https://docs.ansible.com/ansible/devel/plugins/callback.html#plugin-list

--
Kai Stian Olstad

area0

unread,
Dec 3, 2017, 7:07:00 PM12/3/17
to Ansible Project
Thanks Kai - i managed to get exactly what i wanted by using the following environment variable;

ANSIBLE_STDOUT_CALLBACK=debug

Here's the final debug/msg configuration of the playbook;

        - debug:
            msg: |
              {{ inventory_hostname }} has {{ available_updates.found_update_count }} updates available.
              {% for key, value in available_updates.updates.iteritems() %}
              - KB: {{ value.title }}
              {% endfor %}


Now when i run the playbook, i get the following output;

ok: [host1] => {}

MSG:

host1 has 6 updates available.
- KB: Update for Windows Server 2012 R2 (KB3013769)
- KB: Update for Windows Server 2012 R2 (KB3102429)
- KB: Update for Windows Server 2012 R2 (KB3013816)
- KB: Update for Windows Server 2012 R2 (KB3084905)
- KB: 2017-11 Preview of Monthly Quality Rollup for Windows Server 2012 R2 for x64-based Systems (KB4050946)
- KB: Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830)

Thanks for all your help

area0

unread,
Dec 3, 2017, 11:35:20 PM12/3/17
to Ansible Project
Hi,

One last request; what's the best way to get the users confirmation before continuing further with the playbook? I'd like to be able to say, "host1 has 6 updates available.
- KB: Update for Windows Server 2012 R2 (KB3013769)
- KB: Update for Windows Server 2012 R2 (KB3102429)
- KB: Update for Windows Server 2012 R2 (KB3013816)
- KB: Update for Windows Server 2012 R2 (KB3084905)
- KB: 2017-11 Preview of Monthly Quality Rollup for Windows Server 2012 R2 for x64-based Systems (KB4050946)
- KB: Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830)

"Do you wish to install all updates(y/n)?". If the user presses y, the playbook will continue and complete the install + reboot. If the user presses n, the playbook will exit.

I've tried prompts, var_prompts etc; none of which achieve what i'm looking for.

Any advice?

Thank you

Kai Stian Olstad

unread,
Dec 4, 2017, 2:41:27 AM12/4/17
to ansible...@googlegroups.com
On Monday, 4 December 2017 05.35.20 CET area0 wrote:
> One last request; what's the best way to get the users confirmation before
> continuing further with the playbook? I'd like to be able to say, "host1
> has 6 updates available.
> - KB: Update for Windows Server 2012 R2 (KB3013769)
> - KB: Update for Windows Server 2012 R2 (KB3102429)
> - KB: Update for Windows Server 2012 R2 (KB3013816)
> - KB: Update for Windows Server 2012 R2 (KB3084905)
> - KB: 2017-11 Preview of Monthly Quality Rollup for Windows Server 2012 R2
> for x64-based Systems (KB4050946)
> - KB: Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and
> Windows Server 2012, 2012 R2, 2016 x64 Edition - November 2017 (KB890830)
>
> "Do you wish to install all updates(y/n)?". If the user presses y, the
> playbook will continue and complete the install + reboot. If the user
> presses n, the playbook will exit.
>
> I've tried prompts, var_prompts etc; none of which achieve what i'm looking
> for.
>
> Any advice?

I think the only option is the pause module.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages