From d4a07f95731600dad477a6b114dfe425934de021 Mon Sep 17 00:00:00 2001 From: Straff Date: Wed, 13 Feb 2019 22:08:07 +1300 Subject: [PATCH] Added support for user-specified log file in win_package (#51104) * Added support for user-specified log file in win_package - feature (#38353) * added integration tests for win_package log_path support feature (#38353), and applied review feedback * win_package log_path support feature (#38353) - fixed typo in win-package.py documentation * win_package log_path support feature (#38353) - improved an integration test and better doc in win-package.py --- lib/ansible/modules/windows/win_package.ps1 | 21 +++++++++------- lib/ansible/modules/windows/win_package.py | 10 +++++++- .../targets/win_package/defaults/main.yml | 2 ++ .../targets/win_package/tasks/msi_tests.yml | 24 ++++++++++++++++++- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/windows/win_package.ps1 b/lib/ansible/modules/windows/win_package.ps1 index bc8f54c512a..0f5aea7d67d 100644 --- a/lib/ansible/modules/windows/win_package.ps1 +++ b/lib/ansible/modules/windows/win_package.ps1 @@ -25,6 +25,7 @@ $validate_certs = Get-AnsibleParam -obj $params -name "validate_certs" -type "bo $creates_path = Get-AnsibleParam -obj $params -name "creates_path" -type "path" $creates_version = Get-AnsibleParam -obj $params -name "creates_version" -type "str" $creates_service = Get-AnsibleParam -obj $params -name "creates_service" -type "str" +$log_path = Get-AnsibleParam -obj $params -name "log_path" -type "path" $result = @{ changed = $false @@ -326,10 +327,12 @@ if ($state -eq "absent") { if ($program_metadata.msi -eq $true) { # we are uninstalling an msi - $temp_path = [System.IO.Path]::GetTempPath() - $log_file = [System.IO.Path]::GetRandomFileName() - $log_path = Join-Path -Path $temp_path -ChildPath $log_file - $cleanup_artifacts += $log_path + if ( -Not $log_path ) { + $temp_path = [System.IO.Path]::GetTempPath() + $log_file = [System.IO.Path]::GetRandomFileName() + $log_path = Join-Path -Path $temp_path -ChildPath $log_file + $cleanup_artifacts += $log_path + } if ($program_metadata.product_id -ne $null) { $id = $program_metadata.product_id @@ -418,11 +421,13 @@ if ($state -eq "absent") { if ($program_metadata.msi -eq $true) { # we are installing an msi - $temp_path = [System.IO.Path]::GetTempPath() - $log_file = [System.IO.Path]::GetRandomFileName() - $log_path = Join-Path -Path $temp_path -ChildPath $log_file + if ( -Not $log_path ) { + $temp_path = [System.IO.Path]::GetTempPath() + $log_file = [System.IO.Path]::GetRandomFileName() + $log_path = Join-Path -Path $temp_path -ChildPath $log_file + $cleanup_artifacts += $log_path + } - $cleanup_artifacts += $log_path $install_arguments = @("$env:windir\system32\msiexec.exe", "/i", $local_path, "/L*V", $log_path, "/qn", "/norestart") } else { $log_path = $null diff --git a/lib/ansible/modules/windows/win_package.py b/lib/ansible/modules/windows/win_package.py index 19db8895860..d3eebd9e8b5 100644 --- a/lib/ansible/modules/windows/win_package.py +++ b/lib/ansible/modules/windows/win_package.py @@ -130,6 +130,13 @@ options: type: bool default: yes version_added: '2.4' + log_path: + description: + - Specifies the path to a log file that is persisted after an MSI package is installed or uninstalled. + - When omitted, a temporary log file is used for MSI packages. + - This is only valid for MSI files, use C(arguments) for other package types. + type: path + version_added: '2.8' notes: - When C(state=absent) and the product is an exe, the path may be different from what was used to install the package originally. If path is not set then @@ -163,7 +170,7 @@ EXAMPLES = r''' product_id: '{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}' arguments: /install /passive /norestart -- name: Install Visual C thingy with list of arguments instead of a string +- name: Install Visual C thingy with list of arguments instead of a string, and permanent log win_package: path: http://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe product_id: '{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}' @@ -171,6 +178,7 @@ EXAMPLES = r''' - /install - /passive - /norestart + log_path: D:\logs\vcredist_x64-exe-{{lookup('pipe', 'date +%Y%m%dT%H%M%S')}}.log - name: Install Remote Desktop Connection Manager from msi win_package: diff --git a/test/integration/targets/win_package/defaults/main.yml b/test/integration/targets/win_package/defaults/main.yml index fce37d71ae4..cdeff6c40e4 100644 --- a/test/integration/targets/win_package/defaults/main.yml +++ b/test/integration/targets/win_package/defaults/main.yml @@ -2,6 +2,8 @@ # spaces are tricky, let's have one by default test_win_package_path_safe: C:\ansible\win_package test_win_package_path: C:\ansible\win package +test_win_package_log_path_install: C:\ansible\win package\test-install.log +test_win_package_log_path_uninstall: C:\ansible\win package\test-uninstall.log test_win_package_good_url: https://s3.amazonaws.com/ansible-ci-files/test/integration/roles/test_win_package/good.msi test_win_package_reboot_url: https://s3.amazonaws.com/ansible-ci-files/test/integration/roles/test_win_package/reboot.msi test_win_package_bad_url: https://s3.amazonaws.com/ansible-ci-files/test/integration/roles/test_win_package/bad.msi diff --git a/test/integration/targets/win_package/tasks/msi_tests.yml b/test/integration/targets/win_package/tasks/msi_tests.yml index 2e36922b933..f6aeae7706f 100644 --- a/test/integration/targets/win_package/tasks/msi_tests.yml +++ b/test/integration/targets/win_package/tasks/msi_tests.yml @@ -25,10 +25,11 @@ - install_local_msi_check.reboot_required == False - install_local_msi_actual_check.exists == False -- name: install local msi +- name: install local msi with log win_package: path: '{{test_win_package_path}}\good.msi' state: present + log_path: "{{test_win_package_log_path_install}}" register: install_local_msi - name: get result of install local msi @@ -44,6 +45,16 @@ - install_local_msi.rc == 0 - install_local_msi_actual.exists == True +- name: get result of install local msi log_path + win_stat: + path: "{{test_win_package_log_path_install}}" + register: install_local_msi_actual_log_path + +- name: assert install local msi log path + assert: + that: + - install_local_msi_actual_log_path.stat.exists == True + - name: install local msi (idempotent) win_package: path: '{{test_win_package_path}}\good.msi' @@ -78,6 +89,7 @@ win_package: path: '{{test_win_package_path}}\good.msi' state: absent + log_path: "{{test_win_package_log_path_uninstall}}" register: uninstall_path_local_msi - name: get result of uninstall local msi with path @@ -93,6 +105,16 @@ - uninstall_path_local_msi.rc == 0 - uninstall_path_local_msi_actual.exists == False +- name: get result of uninstall local msi with path + win_stat: + path: "{{test_win_package_log_path_uninstall}}" + register: uninstall_path_local_msi_actual_log_path + +- name: assert uninstall local msi with path + assert: + that: + - uninstall_path_local_msi_actual_log_path.stat.exists == True # we expect log to remain + - name: uninstall local msi with path (idempotent) win_package: path: '{{test_win_package_path}}\good.msi'