Consistent path attribute for file-related modules

Not all file-related modules consistently use "path" as the attribute to specify a single filename, some use "dest", others use "name". Most do have aliases for either "name" or "destfile".

This change makes "path" the default attribute for (single) file-related modules, but also adds "dest" and "name" as aliases, so that people can use a consistent way of attributing paths, but also to ensure backward compatibility with existing playbooks.

NOTE: The reason for changing this, is that it makes Ansible needlessly harder to use if you have to remember that e.g. the xattr module requires the name attribute, the lineinfile module requires a dest attribute, and the stat module requires a path attribute.
This commit is contained in:
Dag Wieers 2017-01-03 13:47:00 +01:00 committed by Brian Coca
commit 1ad55ec9de
9 changed files with 215 additions and 200 deletions

View file

@ -25,7 +25,7 @@ $params = Parse-Args $args;
# Initialize defaults for input parameters.
$dest= Get-Attr $params "dest" $FALSE;
$path= Get-Attr $params "path" $FALSE;
$regexp = Get-Attr $params "regexp" $FALSE;
$state = Get-Attr $params "state" "present";
$line = Get-Attr $params "line" $FALSE;
@ -39,32 +39,34 @@ $encoding = Get-Attr $params "encoding" "auto";
$newline = Get-Attr $params "newline" "windows";
# Parse dest / name /destfile param aliases for compatibility with lineinfile
# Parse path / dest / destfile / name param aliases for compatibility with lineinfile
# and fail if at least one spelling of the parameter is not provided.
$dest = Get-Attr $params "dest" $FALSE;
If ($dest -eq $FALSE) {
$dest = Get-Attr $params "name" $FALSE;
If ($dest -eq $FALSE) {
$dest = Get-Attr $params "destfile" $FALSE;
If ($dest -eq $FALSE) {
Fail-Json (New-Object psobject) "missing required argument: dest";
If ($path -eq $FALSE) {
$path = Get-Attr $params "dest" $FALSE;
If ($path -eq $FALSE) {
$path = Get-Attr $params "destfile" $FALSE;
If ($path -eq $FALSE) {
$path = Get-Attr $params "name" $FALSE;
If ($path -eq $FALSE) {
Fail-Json (New-Object psobject) "missing required argument: path";
}
}
}
}
# Fail if the destination is not a file
# Fail if the path is not a file
If (Test-Path $dest -pathType container) {
Fail-Json (New-Object psobject) "destination is a directory";
If (Test-Path $path -pathType container) {
Fail-Json (New-Object psobject) "Path $path is a directory";
}
# Write lines to a file using the specified line separator and encoding,
# performing validation if a validation command was specified.
function WriteLines($outlines, $dest, $linesep, $encodingobj, $validate) {
function WriteLines($outlines, $path, $linesep, $encodingobj, $validate) {
$temppath = [System.IO.Path]::GetTempFileName();
$joined = $outlines -join $linesep;
[System.IO.File]::WriteAllText($temppath, $joined, $encodingobj);
@ -94,9 +96,9 @@ function WriteLines($outlines, $dest, $linesep, $encodingobj, $validate) {
}
# Commit changes to the destination file
$cleandest = $dest.Replace("/", "\");
Copy-Item $temppath $cleandest -force;
# Commit changes to the path
$cleanpath = $path.Replace("/", "\");
Copy-Item $temppath $cleanpath -force;
Remove-Item $temppath -force;
}
@ -113,24 +115,24 @@ function BackupFile($path) {
# Implement the functionality for state == 'present'
function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $backup, $backrefs, $validate, $encodingobj, $linesep) {
function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $backup, $backrefs, $validate, $encodingobj, $linesep) {
# Note that we have to clean up the dest path because ansible wants to treat / and \ as
# Note that we have to clean up the path because ansible wants to treat / and \ as
# interchangeable in windows pathnames, but .NET framework internals do not support that.
$cleandest = $dest.Replace("/", "\");
$cleanpath = $path.Replace("/", "\");
# Check if destination exists. If it does not exist, either create it if create == "yes"
# Check if path exists. If it does not exist, either create it if create == "yes"
# was specified or fail with a reasonable error message.
If (!(Test-Path $dest)) {
If (!(Test-Path $path)) {
If ($create -eq "no") {
Fail-Json (New-Object psobject) "Destination $dest does not exist !";
Fail-Json (New-Object psobject) "Path $path does not exist !";
}
# Create new empty file, using the specified encoding to write correct BOM
[System.IO.File]::WriteAllLines($cleandest, "", $encodingobj);
[System.IO.File]::WriteAllLines($cleanpath, "", $encodingobj);
}
# Read the dest file lines using the indicated encoding into a mutable ArrayList.
$content = [System.IO.File]::ReadAllLines($cleandest, $encodingobj);
$content = [System.IO.File]::ReadAllLines($cleanpath, $encodingobj);
If ($content -eq $null) {
$lines = New-Object System.Collections.ArrayList;
}
@ -225,15 +227,15 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
}
# Write backup file if backup == "yes"
$backupdest = "";
$backuppath = "";
If ($changed -eq $TRUE -and $backup -eq "yes") {
$backupdest = BackupFile $dest;
$backuppath = BackupFile $path;
}
# Write changes to the destination file if changes were made
# Write changes to the path if changes were made
If ($changed) {
WriteLines $lines $dest $linesep $encodingobj $validate;
WriteLines $lines $path $linesep $encodingobj $validate;
}
$encodingstr = $encodingobj.WebName;
@ -242,7 +244,7 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
$result = New-Object psobject @{
changed = $changed
msg = $msg
backup = $backupdest
backup = $backuppath
encoding = $encodingstr
}
@ -252,19 +254,19 @@ function Present($dest, $regexp, $line, $insertafter, $insertbefore, $create, $b
# Implement the functionality for state == 'absent'
function Absent($dest, $regexp, $line, $backup, $validate, $encodingobj, $linesep) {
function Absent($path, $regexp, $line, $backup, $validate, $encodingobj, $linesep) {
# Check if destination exists. If it does not exist, fail with a reasonable error message.
If (!(Test-Path $dest)) {
Fail-Json (New-Object psobject) "Destination $dest does not exist !";
# Check if path exists. If it does not exist, fail with a reasonable error message.
If (!(Test-Path $path)) {
Fail-Json (New-Object psobject) "Path $path does not exist !";
}
# Read the dest file lines using the indicated encoding into a mutable ArrayList. Note
# that we have to clean up the dest path because ansible wants to treat / and \ as
# that we have to clean up the path because ansible wants to treat / and \ as
# interchangeable in windows pathnames, but .NET framework internals do not support that.
$cleandest = $dest.Replace("/", "\");
$content = [System.IO.File]::ReadAllLines($cleandest, $encodingobj);
$cleanpath = $path.Replace("/", "\");
$content = [System.IO.File]::ReadAllLines($cleanpath, $encodingobj);
If ($content -eq $null) {
$lines = New-Object System.Collections.ArrayList;
}
@ -303,15 +305,15 @@ function Absent($dest, $regexp, $line, $backup, $validate, $encodingobj, $linese
}
# Write backup file if backup == "yes"
$backupdest = "";
$backuppath = "";
If ($changed -eq $TRUE -and $backup -eq "yes") {
$backupdest = BackupFile $dest;
$backuppath = BackupFile $path;
}
# Write changes to the destination file if changes were made
# Write changes to the path if changes were made
If ($changed) {
WriteLines $left $dest $linesep $encodingobj $validate;
WriteLines $left $path $linesep $encodingobj $validate;
}
# Return result information
@ -322,7 +324,7 @@ function Absent($dest, $regexp, $line, $backup, $validate, $encodingobj, $linese
$result = New-Object psobject @{
changed = $changed
msg = $msg
backup = $backupdest
backup = $backuppath
found = $fcount
encoding = $encodingstr
}
@ -362,7 +364,7 @@ If ($encoding -ne "auto") {
# Otherwise see if we can determine the current encoding of the target file.
# If the file doesn't exist yet (create == 'yes') we use the default or
# explicitly specified encoding set above.
Elseif (Test-Path $dest) {
Elseif (Test-Path $path) {
# Get a sorted list of encodings with preambles, longest first
@ -381,7 +383,7 @@ Elseif (Test-Path $dest) {
# Get the first N bytes from the file, where N is the max preamble length we saw
[Byte[]]$bom = Get-Content -Encoding Byte -ReadCount $max_preamble_len -TotalCount $max_preamble_len -Path $dest;
[Byte[]]$bom = Get-Content -Encoding Byte -ReadCount $max_preamble_len -TotalCount $max_preamble_len -Path $path;
# Iterate through the sorted encodings, looking for a full match.
@ -426,7 +428,7 @@ If ($state -eq "present") {
$insertafter = "EOF";
}
Present $dest $regexp $line $insertafter $insertbefore $create $backup $backrefs $validate $encodingobj $linesep;
Present $path $regexp $line $insertafter $insertbefore $create $backup $backrefs $validate $encodingobj $linesep;
}
Else {
@ -435,22 +437,5 @@ Else {
Fail-Json (New-Object psobject) "one of line= or regexp= is required with state=absent";
}
Absent $dest $regexp $line $backup $validate $encodingobj $linesep;
Absent $path $regexp $line $backup $validate $encodingobj $linesep;
}