parent
93bb6b8eaf
commit
802fbcadf8
2 changed files with 49 additions and 82 deletions
|
@ -19,97 +19,67 @@
|
|||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$params = Parse-Args $args
|
||||
$params = Parse-Args $args -supports_check_mode $true
|
||||
|
||||
# path
|
||||
$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"
|
||||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
|
||||
|
||||
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -default $false -failifempty $true -aliases "dest","name"
|
||||
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","directory","file","touch"
|
||||
|
||||
$result = @{
|
||||
changed = $false
|
||||
}
|
||||
|
||||
if ($state -eq "touch") {
|
||||
if (-not $check_mode) {
|
||||
if (Test-Path $path) {
|
||||
(Get-ChildItem $path).LastWriteTime = Get-Date
|
||||
} else {
|
||||
echo $null > $path
|
||||
}
|
||||
}
|
||||
$result.changed = $true
|
||||
}
|
||||
|
||||
# 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"
|
||||
# if state is not supplied, test the $path to see if it looks like
|
||||
# a file or a folder and set state to file or folder
|
||||
|
||||
# result
|
||||
$result = New-Object psobject @{
|
||||
changed = $FALSE
|
||||
}
|
||||
|
||||
If ( $state -eq "touch" )
|
||||
{
|
||||
If(Test-Path $path)
|
||||
{
|
||||
(Get-ChildItem $path).LastWriteTime = Get-Date
|
||||
}
|
||||
Else
|
||||
{
|
||||
echo $null > $path
|
||||
}
|
||||
$result.changed = $TRUE
|
||||
}
|
||||
|
||||
If (Test-Path $path)
|
||||
{
|
||||
if (Test-Path $path) {
|
||||
$fileinfo = Get-Item $path
|
||||
If ( $state -eq "absent" )
|
||||
{
|
||||
Remove-Item -Recurse -Force $fileinfo
|
||||
$result.changed = $TRUE
|
||||
}
|
||||
Else
|
||||
{
|
||||
If ( $state -eq "directory" -and -not $fileinfo.PsIsContainer )
|
||||
{
|
||||
Fail-Json (New-Object psobject) "path is not a directory"
|
||||
if ($state -eq "absent") {
|
||||
if (-not $check_mode) {
|
||||
Remove-Item -Recurse -Force $fileinfo
|
||||
}
|
||||
$result.changed = $true
|
||||
} else {
|
||||
if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
|
||||
Fail-Json $result "path $path is not a directory"
|
||||
}
|
||||
|
||||
If ( $state -eq "file" -and $fileinfo.PsIsContainer )
|
||||
{
|
||||
Fail-Json (New-Object psobject) "path is not a file"
|
||||
if ($state -eq "file" -and $fileinfo.PsIsContainer) {
|
||||
Fail-Json $result "path $path is not a file"
|
||||
}
|
||||
}
|
||||
}
|
||||
Else
|
||||
# doesn't yet exist
|
||||
{
|
||||
If ( $state -eq "unspecified" )
|
||||
{
|
||||
|
||||
} else {
|
||||
|
||||
# If state is not supplied, test the $path to see if it looks like
|
||||
# a file or a folder and set state to file or folder
|
||||
if ($state -eq $null) {
|
||||
$basename = Split-Path -Path $path -Leaf
|
||||
If ($basename.length -gt 0)
|
||||
{
|
||||
if ($basename.length -gt 0) {
|
||||
$state = "file"
|
||||
}
|
||||
Else
|
||||
{
|
||||
} else {
|
||||
$state = "directory"
|
||||
}
|
||||
}
|
||||
|
||||
If ( $state -eq "directory" )
|
||||
{
|
||||
New-Item -ItemType directory -Path $path | Out-Null
|
||||
$result.changed = $TRUE
|
||||
if ($state -eq "directory") {
|
||||
if (-not $check_mode) {
|
||||
New-Item -ItemType directory -Path $path | Out-Null
|
||||
}
|
||||
$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
|
||||
|
|
|
@ -31,7 +31,7 @@ short_description: Creates, touches or removes files or directories.
|
|||
description:
|
||||
- Creates (empty) files, updates file modification stamps of existing files,
|
||||
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:
|
||||
- See also M(win_copy), M(win_template), M(copy), M(template), M(assemble)
|
||||
requirements: [ ]
|
||||
|
@ -41,7 +41,6 @@ options:
|
|||
description:
|
||||
- 'path to the file being managed. Aliases: I(dest), I(name)'
|
||||
required: true
|
||||
default: []
|
||||
aliases: ['dest', 'name']
|
||||
state:
|
||||
description:
|
||||
|
@ -53,34 +52,32 @@ options:
|
|||
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
|
||||
modification times (similar to the way C(touch) works from the command line).
|
||||
required: false
|
||||
default: file
|
||||
choices: [ file, directory, touch, absent ]
|
||||
'''
|
||||
|
||||
EXAMPLES = r'''
|
||||
- name: Create a file
|
||||
win_file:
|
||||
path: C:\temp\foo.conf
|
||||
path: C:\Temp\foo.conf
|
||||
state: file
|
||||
|
||||
- name: Touch a file (creates if not present, updates modification time if present)
|
||||
win_file:
|
||||
path: C:\temp\foo.conf
|
||||
path: C:\Temp\foo.conf
|
||||
state: touch
|
||||
|
||||
- name: Remove a file, if present
|
||||
win_file:
|
||||
path: C:\temp\foo.conf
|
||||
path: C:\Temp\foo.conf
|
||||
state: absent
|
||||
|
||||
- name: Create directory structure
|
||||
win_file:
|
||||
path: C:\temp\folder\subfolder
|
||||
path: C:\Temp\folder\subfolder
|
||||
state: directory
|
||||
|
||||
- name: Remove directory structure
|
||||
win_file:
|
||||
path: C:\temp
|
||||
path: C:\Temp
|
||||
state: absent
|
||||
'''
|
||||
|
|
Loading…
Reference in a new issue