mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
parent
93bb6b8eaf
commit
802fbcadf8
2 changed files with 49 additions and 82 deletions
|
@ -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-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"
|
||||||
$path = Get-Attr $params "dest" $FALSE
|
|
||||||
If ($path -eq $FALSE)
|
$result = @{
|
||||||
{
|
changed = $false
|
||||||
$path = Get-Attr $params "name" $FALSE
|
}
|
||||||
If ($path -eq $FALSE)
|
|
||||||
{
|
if ($state -eq "touch") {
|
||||||
Fail-Json (New-Object psobject) "missing required argument: path"
|
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
|
if (Test-Path $path) {
|
||||||
# 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)
|
|
||||||
{
|
|
||||||
$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…
Add table
Reference in a new issue