win modules - standarize regex and regxp as alias (#59037)

* win modules - standarize regex  and regxp as alias

* Changes to reflect desired state

* Update parameter names of funtions

* Update main.yml

* Update win_lineinfile.py

* Update win_wait_for.py
This commit is contained in:
Shachaf92 2019-08-28 02:38:23 +03:00 committed by Jordan Borean
parent 66f52b74b1
commit 03bbba4a9f
7 changed files with 40 additions and 37 deletions

View file

@ -17,7 +17,7 @@ $age_stamp = Get-AnsibleParam -obj $params -name 'age_stamp' -default 'mtime' -V
$file_type = Get-AnsibleParam -obj $params -name 'file_type' -default 'file' -ValidateSet 'file','directory'
$follow = Get-AnsibleParam -obj $params -name 'follow' -type "bool" -default $false
$hidden = Get-AnsibleParam -obj $params -name 'hidden' -type "bool" -default $false
$patterns = Get-AnsibleParam -obj $params -name 'patterns'
$patterns = Get-AnsibleParam -obj $params -name 'patterns' -aliases "regex","regexp"
$recurse = Get-AnsibleParam -obj $params -name 'recurse' -type "bool" -default $false
$size = Get-AnsibleParam -obj $params -name 'size'
$use_regex = Get-AnsibleParam -obj $params -name 'use_regex' -type "bool" -default $false

View file

@ -79,6 +79,7 @@ options:
- The patterns retrict the list of files or folders to be returned based on the filenames.
- For a file to be matched it only has to match with one pattern in a list provided.
type: list
aliases: [ "regex", "regexp" ]
recurse:
description:
- Will recursively descend into the directory looking for files or folders.

View file

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

View file

@ -31,11 +31,12 @@ options:
so you can get the original file back if you somehow clobbered it incorrectly.
type: bool
default: no
regexp:
regex:
description:
- The regular expression to look for in every line of the file. For C(state=present), the pattern to replace if found; only the last line found
will be replaced. For C(state=absent), the pattern of the line to remove. Uses .NET compatible regular expressions;
see U(https://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx).
aliases: [ "regexp" ]
state:
description:
- Whether the line should be there or not.
@ -117,28 +118,28 @@ EXAMPLES = r'''
- win_lineinfile:
path: C:\Temp\example.conf
regexp: '^name='
regex: '^name='
line: 'name=JohnDoe'
- win_lineinfile:
path: C:\Temp\example.conf
regexp: '^name='
regex: '^name='
state: absent
- win_lineinfile:
path: C:\Temp\example.conf
regexp: '^127\.0\.0\.1'
regex: '^127\.0\.0\.1'
line: '127.0.0.1 localhost'
- win_lineinfile:
path: C:\Temp\httpd.conf
regexp: '^Listen '
regex: '^Listen '
insertafter: '^#Listen '
line: Listen 8080
- win_lineinfile:
path: C:\Temp\services
regexp: '^# port for http'
regex: '^# port for http'
insertbefore: '^www.*80/tcp'
line: '# port for http by default'
@ -159,7 +160,7 @@ EXAMPLES = r'''
win_lineinfile:
path: C:\Temp\example.conf
backrefs: yes
regexp: '(^name=)'
regex: '(^name=)'
line: '$1JohnDoe'
'''

View file

@ -16,7 +16,7 @@ $exclude_hosts = Get-AnsibleParam -obj $params -name "exclude_hosts" -type "list
$hostname = Get-AnsibleParam -obj $params -name "host" -type "str" -default "127.0.0.1"
$path = Get-AnsibleParam -obj $params -name "path" -type "path"
$port = Get-AnsibleParam -obj $params -name "port" -type "int"
$search_regex = Get-AnsibleParam -obj $params -name "search_regex" -type "str"
$regex = Get-AnsibleParam -obj $params -name "regex" -type "str" -aliases "search_regex","regexp"
$sleep = Get-AnsibleParam -obj $params -name "sleep" -type "int" -default 1
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "started" -validateset "present","started","stopped","absent","drained"
$timeout = Get-AnsibleParam -obj $params -name "timeout" -type "int" -default 300
@ -44,8 +44,8 @@ if ($null -ne $path) {
}
if ($null -ne $port) {
if ($null -ne $search_regex) {
Fail-Json $result "search_regex should by used when checking a string in a file in the win_wait_for module"
if ($null -ne $regex) {
Fail-Json $result "regex should by used when checking a string in a file in the win_wait_for module"
}
if ($null -ne $exclude_hosts -and $state -ne "drained") {
@ -112,12 +112,12 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-AnsiblePath -Path $path) {
if ($null -eq $search_regex) {
if ($null -eq $regex) {
$complete = $true
break
} else {
$file_contents = Get-Content -Path $path -Raw
if ($file_contents -match $search_regex) {
if ($file_contents -match $regex) {
$complete = $true
break
}
@ -129,10 +129,10 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
if ($complete -eq $false) {
$result.elapsed = ((Get-Date) - $module_start).TotalSeconds
$result.wait_attempts = $attempts
if ($null -eq $search_regex) {
if ($null -eq $regex) {
Fail-Json $result "timeout while waiting for file $path to be present"
} else {
Fail-Json $result "timeout while waiting for string regex $search_regex in file $path to match"
Fail-Json $result "timeout while waiting for string regex $regex in file $path to match"
}
}
} elseif ($state -in @("absent")) {
@ -142,9 +142,9 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-AnsiblePath -Path $path) {
if ($null -ne $search_regex) {
if ($null -ne $regex) {
$file_contents = Get-Content -Path $path -Raw
if ($file_contents -notmatch $search_regex) {
if ($file_contents -notmatch $regex) {
$complete = $true
break
}
@ -160,10 +160,10 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
if ($complete -eq $false) {
$result.elapsed = ((Get-Date) - $module_start).TotalSeconds
$result.wait_attempts = $attempts
if ($null -eq $search_regex) {
if ($null -eq $regex) {
Fail-Json $result "timeout while waiting for file $path to be absent"
} else {
Fail-Json $result "timeout while waiting for string regex $search_regex in file $path to not match"
Fail-Json $result "timeout while waiting for string regex $regex in file $path to not match"
}
}
}

View file

@ -61,7 +61,7 @@ options:
description:
- The port number to poll on C(host).
type: int
search_regex:
regex:
description:
- Can be used to match a string in a file.
- If C(state) is present or started then it will wait until the regex
@ -69,6 +69,7 @@ options:
- If C(state) is absent then it will wait until the regex does not match.
- Defaults to a multiline regex.
type: str
aliases: [ "search_regex", "regexp" ]
sleep:
description:
- Number of seconds to sleep between checks.
@ -124,7 +125,7 @@ EXAMPLES = r'''
- name: Wait until process complete is in the file before continuing
win_wait_for:
path: C:\temp\log.txt
search_regex: process complete
regex: process complete
- name: Wait until file is removed
win_wait_for:

View file

@ -49,7 +49,7 @@
port: 0
search_regex: a
register: fail_port_search_regex
failed_when: fail_port_search_regex.msg != 'search_regex should by used when checking a string in a file in the win_wait_for module'
failed_when: fail_port_search_regex.msg != 'regex should by used when checking a string in a file in the win_wait_for module'
- name: fail to set exclude_hosts with port whens tate is not drained
win_wait_for: