win_updates: Add flag to only download updates without installing them (#58631)

* win_updates: Add flag to only download updates without installing them

* Fix test

* Fixes ansible-test (pep8)

* Fix integration test

* Fix actual fix.
pull/52146/head
Ruben-Bosch 5 years ago committed by Jordan Borean
parent 03bbba4a9f
commit 5549788c8d

@ -13,7 +13,7 @@ $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "b
$category_names = Get-AnsibleParam -obj $params -name "category_names" -type "list" -default @("CriticalUpdates", "SecurityUpdates", "UpdateRollups") $category_names = Get-AnsibleParam -obj $params -name "category_names" -type "list" -default @("CriticalUpdates", "SecurityUpdates", "UpdateRollups")
$log_path = Get-AnsibleParam -obj $params -name "log_path" -type "path" $log_path = Get-AnsibleParam -obj $params -name "log_path" -type "path"
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "installed" -validateset "installed", "searched" $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "installed" -validateset "installed", "searched", "downloaded"
$blacklist = Get-AnsibleParam -obj $params -name "blacklist" -type "list" $blacklist = Get-AnsibleParam -obj $params -name "blacklist" -type "list"
$whitelist = Get-AnsibleParam -obj $params -name "whitelist" -type "list" $whitelist = Get-AnsibleParam -obj $params -name "whitelist" -type "list"
$server_selection = Get-AnsibleParam -obj $params -name "server_selection" -type "string" -default "default" -validateset "default", "managed_server", "windows_update" $server_selection = Get-AnsibleParam -obj $params -name "server_selection" -type "string" -default "default" -validateset "default", "managed_server", "windows_update"
@ -296,6 +296,14 @@ $update_script_block = {
$update_index++ $update_index++
} }
# Early exit for download-only
if ($state -eq "downloaded") {
Write-DebugLog -msg "Downloaded $($updates_to_install.Count) updates..."
$result.failed = $false
$result.msg = "Downloaded $($updates_to_install.Count) updates"
return $result
}
Write-DebugLog -msg "Installing updates..." Write-DebugLog -msg "Installing updates..."
# install as a batch so the reboot manager will suppress intermediate reboots # install as a batch so the reboot manager will suppress intermediate reboots

@ -78,10 +78,10 @@ options:
version_added: '2.8' version_added: '2.8'
state: state:
description: description:
- Controls whether found updates are returned as a list or actually installed. - Controls whether found updates are downloaded or installed or listed
- This module also supports Ansible check mode, which has the same effect as setting state=searched - This module also supports Ansible check mode, which has the same effect as setting state=searched
type: str type: str
choices: [ installed, searched ] choices: [ installed, searched, downloaded ]
default: installed default: installed
log_path: log_path:
description: description:
@ -188,6 +188,11 @@ EXAMPLES = r'''
win_updates: win_updates:
reboot: yes reboot: yes
reboot_timeout: 3600 reboot_timeout: 3600
# Search and download Windows updates
- name: Search and download Windows updates without installing them
win_updates:
state: downloaded
''' '''
RETURN = r''' RETURN = r'''
@ -254,7 +259,7 @@ found_update_count:
type: int type: int
sample: 3 sample: 3
installed_update_count: installed_update_count:
description: The number of updates successfully installed. description: The number of updates successfully installed or downloaded.
returned: success returned: success
type: int type: int
sample: 2 sample: 2

@ -141,9 +141,9 @@ class ActionModule(ActionBase):
use_task = boolean(self._task.args.get('use_scheduled_task', False), use_task = boolean(self._task.args.get('use_scheduled_task', False),
strict=False) strict=False)
if state not in ['installed', 'searched']: if state not in ['installed', 'searched', 'downloaded']:
result['failed'] = True result['failed'] = True
result['msg'] = "state must be either installed or searched" result['msg'] = "state must be either installed, searched or downloaded"
return result return result
try: try:

@ -3,7 +3,7 @@
win_updates: win_updates:
state: invalid state: invalid
register: invalid_state register: invalid_state
failed_when: invalid_state.msg != 'state must be either installed or searched' failed_when: invalid_state.msg != 'state must be either installed, searched or downloaded'
- name: ensure log file not present before tests - name: ensure log file not present before tests
win_file: win_file:

@ -19,7 +19,7 @@ class TestWinUpdatesActionPlugin(object):
( (
{"state": "invalid"}, {"state": "invalid"},
False, False,
"state must be either installed or searched" "state must be either installed, searched or downloaded"
), ),
( (
{"reboot": "nonsense"}, {"reboot": "nonsense"},

Loading…
Cancel
Save