win_file: Add check-mode support (#20671)

Also a small cleanup
pull/20785/head
Dag Wieers 8 years ago committed by Matt Davis
parent 93bb6b8eaf
commit 802fbcadf8

@ -19,97 +19,67 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$params = Parse-Args $args $params = Parse-Args $args -supports_check_mode $true
# path $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
$path = Get-Attr $params "path" $FALSE
If ($path -eq $FALSE)
{
$path = Get-Attr $params "dest" $FALSE
If ($path -eq $FALSE)
{
$path = Get-Attr $params "name" $FALSE
If ($path -eq $FALSE)
{
Fail-Json (New-Object psobject) "missing required argument: path"
}
}
}
# JH Following advice from Chris Church, only allow the following states
# in the windows version for now:
# state - file, directory, touch, absent
# (originally was: state - file, link, directory, hard, touch, absent)
$state = Get-Attr $params "state" "unspecified" $path = Get-AnsibleParam -obj $params -name "path" -type "path" -default $false -failifempty $true -aliases "dest","name"
# if state is not supplied, test the $path to see if it looks like $state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","directory","file","touch"
# a file or a folder and set state to file or folder
# result $result = @{
$result = New-Object psobject @{ changed = $false
changed = $FALSE
} }
If ( $state -eq "touch" ) if ($state -eq "touch") {
{ if (-not $check_mode) {
If(Test-Path $path) if (Test-Path $path) {
{ (Get-ChildItem $path).LastWriteTime = Get-Date
(Get-ChildItem $path).LastWriteTime = Get-Date } else {
} echo $null > $path
Else }
{
echo $null > $path
} }
$result.changed = $TRUE $result.changed = $true
} }
If (Test-Path $path) if (Test-Path $path) {
{
$fileinfo = Get-Item $path $fileinfo = Get-Item $path
If ( $state -eq "absent" ) if ($state -eq "absent") {
{ if (-not $check_mode) {
Remove-Item -Recurse -Force $fileinfo Remove-Item -Recurse -Force $fileinfo
$result.changed = $TRUE }
} $result.changed = $true
Else } else {
{ if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
If ( $state -eq "directory" -and -not $fileinfo.PsIsContainer ) Fail-Json $result "path $path is not a directory"
{
Fail-Json (New-Object psobject) "path is not a directory"
} }
If ( $state -eq "file" -and $fileinfo.PsIsContainer ) if ($state -eq "file" -and $fileinfo.PsIsContainer) {
{ Fail-Json $result "path $path is not a file"
Fail-Json (New-Object psobject) "path is not a file"
} }
} }
}
Else } else {
# doesn't yet exist
{ # If state is not supplied, test the $path to see if it looks like
If ( $state -eq "unspecified" ) # a file or a folder and set state to file or folder
{ if ($state -eq $null) {
$basename = Split-Path -Path $path -Leaf $basename = Split-Path -Path $path -Leaf
If ($basename.length -gt 0) if ($basename.length -gt 0) {
{
$state = "file" $state = "file"
} } else {
Else
{
$state = "directory" $state = "directory"
} }
} }
If ( $state -eq "directory" ) if ($state -eq "directory") {
{ if (-not $check_mode) {
New-Item -ItemType directory -Path $path | Out-Null New-Item -ItemType directory -Path $path | Out-Null
$result.changed = $TRUE }
$result.changed = $true
} elseif ($state -eq "file") {
Fail-Json $result "path $path will not be created"
} }
If ( $state -eq "file" )
{
Fail-Json (New-Object psobject) "path will not be created"
}
} }
Exit-Json $result Exit-Json $result

@ -31,7 +31,7 @@ short_description: Creates, touches or removes files or directories.
description: description:
- Creates (empty) files, updates file modification stamps of existing files, - Creates (empty) files, updates file modification stamps of existing files,
and can create or remove directories. and can create or remove directories.
Unlike M(file), does not modify ownership, permissions or manipulate links. - Unlike M(file), does not modify ownership, permissions or manipulate links.
notes: notes:
- See also M(win_copy), M(win_template), M(copy), M(template), M(assemble) - See also M(win_copy), M(win_template), M(copy), M(template), M(assemble)
requirements: [ ] requirements: [ ]
@ -41,7 +41,6 @@ options:
description: description:
- 'path to the file being managed. Aliases: I(dest), I(name)' - 'path to the file being managed. Aliases: I(dest), I(name)'
required: true required: true
default: []
aliases: ['dest', 'name'] aliases: ['dest', 'name']
state: state:
description: description:
@ -53,34 +52,32 @@ options:
If C(touch), an empty file will be created if the C(path) does not If C(touch), an empty file will be created if the C(path) does not
exist, while an existing file or directory will receive updated file access and exist, while an existing file or directory will receive updated file access and
modification times (similar to the way C(touch) works from the command line). modification times (similar to the way C(touch) works from the command line).
required: false
default: file
choices: [ file, directory, touch, absent ] choices: [ file, directory, touch, absent ]
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Create a file - name: Create a file
win_file: win_file:
path: C:\temp\foo.conf path: C:\Temp\foo.conf
state: file state: file
- name: Touch a file (creates if not present, updates modification time if present) - name: Touch a file (creates if not present, updates modification time if present)
win_file: win_file:
path: C:\temp\foo.conf path: C:\Temp\foo.conf
state: touch state: touch
- name: Remove a file, if present - name: Remove a file, if present
win_file: win_file:
path: C:\temp\foo.conf path: C:\Temp\foo.conf
state: absent state: absent
- name: Create directory structure - name: Create directory structure
win_file: win_file:
path: C:\temp\folder\subfolder path: C:\Temp\folder\subfolder
state: directory state: directory
- name: Remove directory structure - name: Remove directory structure
win_file: win_file:
path: C:\temp path: C:\Temp
state: absent state: absent
''' '''

Loading…
Cancel
Save