summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS <S.konijn@true.nl>2019-03-08 06:32:32 +0100
committerToshio Kuratomi <a.badger@gmail.com>2019-03-11 11:53:23 -0700
commitedd7e53b8b762158122d5cc883a007ed4f96c5a6 (patch)
tree8a13a3a484510fa0ba31ee16d1509dd1375331a5
parente0294275d7945f12584cb4fad3e460c295ffb445 (diff)
downloadansible-edd7e53b8b762158122d5cc883a007ed4f96c5a6.tar.gz
Fixed win_file crash with hidden files (#52584)
* Fixed crash with hidden files added "-force" parameter on "Get-Item" cmdlet. this is needed to get file info if the file is "hidden" without this option modules like win_file, win_template, win_copy crashes on hidden files. this is because with "test-path" it sees that the file exists, but "get-item" can't get the file info. for more information on "-force option": https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-item * Add changelog and integration tests * fix tests for older Windows versions (cherry picked from commit 3bc474bf99ad2109262366809bd735deef1312ee)
-rw-r--r--changelogs/fragments/win_file-hidden.yaml2
-rw-r--r--lib/ansible/modules/windows/win_file.ps12
-rw-r--r--test/integration/targets/win_file/tasks/main.yml21
3 files changed, 24 insertions, 1 deletions
diff --git a/changelogs/fragments/win_file-hidden.yaml b/changelogs/fragments/win_file-hidden.yaml
new file mode 100644
index 0000000000..f13b77eb1a
--- /dev/null
+++ b/changelogs/fragments/win_file-hidden.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+- win_file - Fix issue when managing hidden files and directories - https://github.com/ansible/ansible/issues/42466
diff --git a/lib/ansible/modules/windows/win_file.ps1 b/lib/ansible/modules/windows/win_file.ps1
index 06f03494fa..e76f96410b 100644
--- a/lib/ansible/modules/windows/win_file.ps1
+++ b/lib/ansible/modules/windows/win_file.ps1
@@ -102,7 +102,7 @@ if ($state -eq "touch") {
}
if (Test-Path -LiteralPath $path) {
- $fileinfo = Get-Item -LiteralPath $path
+ $fileinfo = Get-Item -LiteralPath $path -Force
if ($state -eq "absent") {
Remove-File -file $fileinfo -checkmode $check_mode
$result.changed = $true
diff --git a/test/integration/targets/win_file/tasks/main.yml b/test/integration/targets/win_file/tasks/main.yml
index fe77a22c77..c528888665 100644
--- a/test/integration/targets/win_file/tasks/main.yml
+++ b/test/integration/targets/win_file/tasks/main.yml
@@ -568,6 +568,27 @@
- file_result.changed
- "stat_result.stat.exists == False"
+# Need to use shell to create the file as win_file doesn't support setting attributes
+- name: create hidden file
+ win_shell: $file = New-Item -Path "{{ win_output_dir }}\hidden.txt" -ItemType File; $file.Attributes = "Hidden"
+
+- name: delete hidden file
+ win_file:
+ path: '{{ win_output_dir }}\hidden.txt'
+ state: absent
+ register: delete_hidden
+
+- name: get result of delete hidden file
+ win_stat:
+ path: '{{ win_output_dir }}\hidden.txt'
+ register: delete_hidden_actual
+
+- name: assert delete hidden file
+ assert:
+ that:
+ - delete_hidden is changed
+ - not delete_hidden_actual.stat.exists
+
- name: create folder to point set symbolic link for
win_file:
path: "{{win_output_dir}}/link-test/link-target"