|
|
|
@ -62,7 +62,7 @@ function WriteLines($outlines, $path, $linesep, $encodingobj, $validate, $check_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Implement the functionality for state == 'present'
|
|
|
|
|
function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $backup, $backrefs, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {
|
|
|
|
|
function Present($path, $regex, $line, $insertafter, $insertbefore, $create, $backup, $backrefs, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
@ -102,8 +102,8 @@ function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $b
|
|
|
|
|
|
|
|
|
|
# Compile the regex specified, if provided
|
|
|
|
|
$mre = $null;
|
|
|
|
|
If ($regexp) {
|
|
|
|
|
$mre = New-Object Regex $regexp, 'Compiled';
|
|
|
|
|
If ($regex) {
|
|
|
|
|
$mre = New-Object Regex $regex, 'Compiled';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Compile the regex for insertafter or insertbefore, if provided
|
|
|
|
@ -115,7 +115,7 @@ function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $b
|
|
|
|
|
$insre = New-Object Regex $insertbefore, 'Compiled';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# index[0] is the line num where regexp has been found
|
|
|
|
|
# index[0] is the line num where regex has been found
|
|
|
|
|
# index[1] is the line num where insertafter/insertbefore has been found
|
|
|
|
|
$index = -1, -1;
|
|
|
|
|
$lineno = 0;
|
|
|
|
@ -125,7 +125,7 @@ function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $b
|
|
|
|
|
|
|
|
|
|
# Iterate through the lines in the file looking for matches
|
|
|
|
|
Foreach ($cur_line in $lines) {
|
|
|
|
|
If ($regexp) {
|
|
|
|
|
If ($regex) {
|
|
|
|
|
$m = $mre.Match($cur_line);
|
|
|
|
|
$match_found = $m.Success;
|
|
|
|
|
If ($match_found) {
|
|
|
|
@ -151,7 +151,7 @@ function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $b
|
|
|
|
|
|
|
|
|
|
If ($index[0] -ne -1) {
|
|
|
|
|
If ($backrefs) {
|
|
|
|
|
$new_line = [regex]::Replace($matched_line, $regexp, $line);
|
|
|
|
|
$new_line = [regex]::Replace($matched_line, $regex, $line);
|
|
|
|
|
}
|
|
|
|
|
Else {
|
|
|
|
|
$new_line = $line;
|
|
|
|
@ -213,7 +213,7 @@ function Present($path, $regexp, $line, $insertafter, $insertbefore, $create, $b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Implement the functionality for state == 'absent'
|
|
|
|
|
function Absent($path, $regexp, $line, $backup, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {
|
|
|
|
|
function Absent($path, $regex, $line, $backup, $validate, $encodingobj, $linesep, $check_mode, $diff_support) {
|
|
|
|
|
|
|
|
|
|
# Check if path exists. If it does not exist, fail with a reasonable error message.
|
|
|
|
|
If (-not (Test-Path -LiteralPath $path)) {
|
|
|
|
@ -247,15 +247,15 @@ function Absent($path, $regexp, $line, $backup, $validate, $encodingobj, $linese
|
|
|
|
|
|
|
|
|
|
# Compile the regex specified, if provided
|
|
|
|
|
$cre = $null;
|
|
|
|
|
If ($regexp) {
|
|
|
|
|
$cre = New-Object Regex $regexp, 'Compiled';
|
|
|
|
|
If ($regex) {
|
|
|
|
|
$cre = New-Object Regex $regex, 'Compiled';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$found = New-Object System.Collections.ArrayList;
|
|
|
|
|
$left = New-Object System.Collections.ArrayList;
|
|
|
|
|
|
|
|
|
|
Foreach ($cur_line in $lines) {
|
|
|
|
|
If ($regexp) {
|
|
|
|
|
If ($regex) {
|
|
|
|
|
$m = $cre.Match($cur_line);
|
|
|
|
|
$match_found = $m.Success;
|
|
|
|
|
}
|
|
|
|
@ -311,7 +311,7 @@ $diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool"
|
|
|
|
|
|
|
|
|
|
# Initialize defaults for input parameters.
|
|
|
|
|
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -aliases "dest","destfile","name";
|
|
|
|
|
$regexp = Get-AnsibleParam -obj $params -name "regexp" -type "str";
|
|
|
|
|
$regex = Get-AnsibleParam -obj $params -name "regex" -type "str" -aliases "regexp";
|
|
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent";
|
|
|
|
|
$line = Get-AnsibleParam -obj $params -name "line" -type "str";
|
|
|
|
|
$backrefs = Get-AnsibleParam -obj $params -name "backrefs" -type "bool" -default $false;
|
|
|
|
@ -395,7 +395,7 @@ ElseIf (Test-Path -LiteralPath $path) {
|
|
|
|
|
# call the appropriate handler function.
|
|
|
|
|
If ($state -eq "present") {
|
|
|
|
|
|
|
|
|
|
If ($backrefs -and -not $regexp) {
|
|
|
|
|
If ($backrefs -and -not $regex) {
|
|
|
|
|
Fail-Json @{} "regexp= is required with backrefs=true";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -413,7 +413,7 @@ If ($state -eq "present") {
|
|
|
|
|
|
|
|
|
|
$present_params = @{
|
|
|
|
|
path = $path
|
|
|
|
|
regexp = $regexp
|
|
|
|
|
regex = $regex
|
|
|
|
|
line = $line
|
|
|
|
|
insertafter = $insertafter
|
|
|
|
|
insertbefore = $insertbefore
|
|
|
|
@ -431,13 +431,13 @@ If ($state -eq "present") {
|
|
|
|
|
}
|
|
|
|
|
ElseIf ($state -eq "absent") {
|
|
|
|
|
|
|
|
|
|
If (-not $regexp -and -not $line) {
|
|
|
|
|
If (-not $regex -and -not $line) {
|
|
|
|
|
Fail-Json @{} "one of line= or regexp= is required with state=absent";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$absent_params = @{
|
|
|
|
|
path = $path
|
|
|
|
|
regexp = $regexp
|
|
|
|
|
regex = $regex
|
|
|
|
|
line = $line
|
|
|
|
|
backup = $backup
|
|
|
|
|
validate = $validate
|
|
|
|
|