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
pull/52146/head
Shachaf92 6 years ago committed by Jordan Borean
parent 66f52b74b1
commit 03bbba4a9f

@ -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' $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 $follow = Get-AnsibleParam -obj $params -name 'follow' -type "bool" -default $false
$hidden = Get-AnsibleParam -obj $params -name 'hidden' -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 $recurse = Get-AnsibleParam -obj $params -name 'recurse' -type "bool" -default $false
$size = Get-AnsibleParam -obj $params -name 'size' $size = Get-AnsibleParam -obj $params -name 'size'
$use_regex = Get-AnsibleParam -obj $params -name 'use_regex' -type "bool" -default $false $use_regex = Get-AnsibleParam -obj $params -name 'use_regex' -type "bool" -default $false

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

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

@ -31,11 +31,12 @@ options:
so you can get the original file back if you somehow clobbered it incorrectly. so you can get the original file back if you somehow clobbered it incorrectly.
type: bool type: bool
default: no default: no
regexp: regex:
description: 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 - 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; 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). see U(https://msdn.microsoft.com/en-us/library/hs600312%28v=vs.110%29.aspx).
aliases: [ "regexp" ]
state: state:
description: description:
- Whether the line should be there or not. - Whether the line should be there or not.
@ -117,28 +118,28 @@ EXAMPLES = r'''
- win_lineinfile: - win_lineinfile:
path: C:\Temp\example.conf path: C:\Temp\example.conf
regexp: '^name=' regex: '^name='
line: 'name=JohnDoe' line: 'name=JohnDoe'
- win_lineinfile: - win_lineinfile:
path: C:\Temp\example.conf path: C:\Temp\example.conf
regexp: '^name=' regex: '^name='
state: absent state: absent
- win_lineinfile: - win_lineinfile:
path: C:\Temp\example.conf path: C:\Temp\example.conf
regexp: '^127\.0\.0\.1' regex: '^127\.0\.0\.1'
line: '127.0.0.1 localhost' line: '127.0.0.1 localhost'
- win_lineinfile: - win_lineinfile:
path: C:\Temp\httpd.conf path: C:\Temp\httpd.conf
regexp: '^Listen ' regex: '^Listen '
insertafter: '^#Listen ' insertafter: '^#Listen '
line: Listen 8080 line: Listen 8080
- win_lineinfile: - win_lineinfile:
path: C:\Temp\services path: C:\Temp\services
regexp: '^# port for http' regex: '^# port for http'
insertbefore: '^www.*80/tcp' insertbefore: '^www.*80/tcp'
line: '# port for http by default' line: '# port for http by default'
@ -159,7 +160,7 @@ EXAMPLES = r'''
win_lineinfile: win_lineinfile:
path: C:\Temp\example.conf path: C:\Temp\example.conf
backrefs: yes backrefs: yes
regexp: '(^name=)' regex: '(^name=)'
line: '$1JohnDoe' line: '$1JohnDoe'
''' '''

@ -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" $hostname = Get-AnsibleParam -obj $params -name "host" -type "str" -default "127.0.0.1"
$path = Get-AnsibleParam -obj $params -name "path" -type "path" $path = Get-AnsibleParam -obj $params -name "path" -type "path"
$port = Get-AnsibleParam -obj $params -name "port" -type "int" $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 $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" $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 $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 $port) {
if ($null -ne $search_regex) { if ($null -ne $regex) {
Fail-Json $result "search_regex should by used when checking a string in a file in the win_wait_for module" 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") { 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) { while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1 $attempts += 1
if (Test-AnsiblePath -Path $path) { if (Test-AnsiblePath -Path $path) {
if ($null -eq $search_regex) { if ($null -eq $regex) {
$complete = $true $complete = $true
break break
} else { } else {
$file_contents = Get-Content -Path $path -Raw $file_contents = Get-Content -Path $path -Raw
if ($file_contents -match $search_regex) { if ($file_contents -match $regex) {
$complete = $true $complete = $true
break break
} }
@ -129,10 +129,10 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
if ($complete -eq $false) { if ($complete -eq $false) {
$result.elapsed = ((Get-Date) - $module_start).TotalSeconds $result.elapsed = ((Get-Date) - $module_start).TotalSeconds
$result.wait_attempts = $attempts $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" Fail-Json $result "timeout while waiting for file $path to be present"
} else { } 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")) { } 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) { while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1 $attempts += 1
if (Test-AnsiblePath -Path $path) { if (Test-AnsiblePath -Path $path) {
if ($null -ne $search_regex) { if ($null -ne $regex) {
$file_contents = Get-Content -Path $path -Raw $file_contents = Get-Content -Path $path -Raw
if ($file_contents -notmatch $search_regex) { if ($file_contents -notmatch $regex) {
$complete = $true $complete = $true
break break
} }
@ -160,10 +160,10 @@ if ($null -eq $path -and $null -eq $port -and $state -ne "drained") {
if ($complete -eq $false) { if ($complete -eq $false) {
$result.elapsed = ((Get-Date) - $module_start).TotalSeconds $result.elapsed = ((Get-Date) - $module_start).TotalSeconds
$result.wait_attempts = $attempts $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" Fail-Json $result "timeout while waiting for file $path to be absent"
} else { } 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"
} }
} }
} }

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

@ -49,7 +49,7 @@
port: 0 port: 0
search_regex: a search_regex: a
register: fail_port_search_regex 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 - name: fail to set exclude_hosts with port whens tate is not drained
win_wait_for: win_wait_for:

Loading…
Cancel
Save