|
|
@ -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
|
|
|
|