|
|
@ -3,36 +3,39 @@
|
|
|
|
# Copyright: (c) 2018, Micah Hunsberger (@mhunsber)
|
|
|
|
# Copyright: (c) 2018, Micah Hunsberger (@mhunsber)
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
|
|
|
#AnsibleRequires -CSharpUtil Ansible.Basic
|
|
|
|
|
|
|
|
|
|
|
|
Set-StrictMode -Version 2
|
|
|
|
Set-StrictMode -Version 2
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
|
|
|
|
|
|
|
$params = Parse-Args -arguments $args -supports_check_mode $true
|
|
|
|
$spec = @{
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
|
|
|
options = @{
|
|
|
|
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false
|
|
|
|
state = @{ type = "str"; choices = "absent", "present"; default = "present" }
|
|
|
|
|
|
|
|
aliases = @{ type = "list"; elements = "str" }
|
|
|
|
|
|
|
|
canonical_name = @{ type = "str" }
|
|
|
|
|
|
|
|
ip_address = @{ type = "str" }
|
|
|
|
|
|
|
|
action = @{ type = "str"; choices = "add", "remove", "set"; default = "set" }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
required_if = @(,@( "state", "present", @("canonical_name", "ip_address")))
|
|
|
|
|
|
|
|
supports_check_mode = $true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
|
|
|
|
|
|
|
|
|
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
|
|
|
|
$state = $module.Params.state
|
|
|
|
$aliases = Get-AnsibleParam -obj $params -name "aliases" -type "list" -failifempty $false
|
|
|
|
$aliases = $module.Params.aliases
|
|
|
|
$canonical_name = Get-AnsibleParam -obj $params -name "canonical_name" -type "str" -failifempty ($state -eq 'present')
|
|
|
|
$canonical_name = $module.Params.canonical_name
|
|
|
|
$ip_address = Get-AnsibleParam -obj $params -name "ip_address" -type "str" -default "" -failifempty ($state -eq 'present')
|
|
|
|
$ip_address = $module.Params.ip_address
|
|
|
|
$action = Get-AnsibleParam -obj $params -name "action" -type "str" -default "set" -validateset "add","remove","set"
|
|
|
|
$action = $module.Params.action
|
|
|
|
|
|
|
|
|
|
|
|
$tmp = [ipaddress]::None
|
|
|
|
$tmp = [ipaddress]::None
|
|
|
|
if($ip_address -and -not [ipaddress]::TryParse($ip_address, [ref]$tmp)){
|
|
|
|
if($ip_address -and -not [ipaddress]::TryParse($ip_address, [ref]$tmp)){
|
|
|
|
Fail-Json -obj @{} -message "win_hosts: Argument ip_address needs to be a valid ip address, but was $ip_address"
|
|
|
|
$module.FailJson("win_hosts: Argument ip_address needs to be a valid ip address, but was $ip_address")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ip_address_type = $tmp.AddressFamily
|
|
|
|
$ip_address_type = $tmp.AddressFamily
|
|
|
|
|
|
|
|
|
|
|
|
$hosts_file = Get-Item -LiteralPath "$env:SystemRoot\System32\drivers\etc\hosts"
|
|
|
|
$hosts_file = Get-Item -LiteralPath "$env:SystemRoot\System32\drivers\etc\hosts"
|
|
|
|
|
|
|
|
|
|
|
|
$result = @{
|
|
|
|
|
|
|
|
changed = $false
|
|
|
|
|
|
|
|
diff = @{
|
|
|
|
|
|
|
|
prepared = ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-CommentIndex($line) {
|
|
|
|
Function Get-CommentIndex($line) {
|
|
|
|
$c_index = $line.IndexOf('#')
|
|
|
|
$c_index = $line.IndexOf('#')
|
|
|
|
if($c_index -lt 0) {
|
|
|
|
if($c_index -lt 0) {
|
|
|
@ -79,30 +82,14 @@ Function Find-HostName($line, $name) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Remove-HostEntry($list, $idx) {
|
|
|
|
Function Remove-HostEntry($list, $idx) {
|
|
|
|
$result.changed = $true
|
|
|
|
$module.Result.changed = $true
|
|
|
|
$removed = $false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($diff_mode) {
|
|
|
|
|
|
|
|
$result.diff.prepared += "`n-[$($list[$idx])]`n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(-not $check_mode) {
|
|
|
|
|
|
|
|
$list.RemoveAt($idx)
|
|
|
|
$list.RemoveAt($idx)
|
|
|
|
$removed = $true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $removed
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Add-HostEntry($list, $cname, $aliases, $ip) {
|
|
|
|
Function Add-HostEntry($list, $cname, $aliases, $ip) {
|
|
|
|
$result.changed = $true
|
|
|
|
$module.Result.changed = $true
|
|
|
|
$line = "$ip $cname $($aliases -join ' ')"
|
|
|
|
$line = "$ip $cname $($aliases -join ' ')"
|
|
|
|
if($diff_mode) {
|
|
|
|
|
|
|
|
$result.diff.prepared += "`n+[$line]`n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(-not $check_mode) {
|
|
|
|
|
|
|
|
$list.Add($line) | Out-Null
|
|
|
|
$list.Add($line) | Out-Null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Remove-HostnamesFromEntry($list, $idx, $aliases) {
|
|
|
|
Function Remove-HostnamesFromEntry($list, $idx, $aliases) {
|
|
|
@ -115,13 +102,8 @@ Function Remove-HostnamesFromEntry($list, $idx, $aliases) {
|
|
|
|
$line = $line.Remove($match.Index + 1, $match.Length -1)
|
|
|
|
$line = $line.Remove($match.Index + 1, $match.Length -1)
|
|
|
|
# was this the last alias? (check for space characters after trimming)
|
|
|
|
# was this the last alias? (check for space characters after trimming)
|
|
|
|
if($line.Substring(0,(Get-CommentIndex -line $line)).Trim() -inotmatch "\s") {
|
|
|
|
if($line.Substring(0,(Get-CommentIndex -line $line)).Trim() -inotmatch "\s") {
|
|
|
|
if($diff_mode){
|
|
|
|
|
|
|
|
$result.diff.prepared += "`n-[$($list[$idx])]`n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(-not $check_mode) {
|
|
|
|
|
|
|
|
$list.RemoveAt($idx)
|
|
|
|
$list.RemoveAt($idx)
|
|
|
|
$line_removed = $true
|
|
|
|
$line_removed = $true
|
|
|
|
}
|
|
|
|
|
|
|
|
# we're done
|
|
|
|
# we're done
|
|
|
|
return @{
|
|
|
|
return @{
|
|
|
|
line_removed = $line_removed
|
|
|
|
line_removed = $line_removed
|
|
|
@ -130,14 +112,9 @@ Function Remove-HostnamesFromEntry($list, $idx, $aliases) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($line -ne $list[$idx]){
|
|
|
|
if($line -ne $list[$idx]){
|
|
|
|
$result.changed = $true
|
|
|
|
$module.Result.changed = $true
|
|
|
|
if($diff_mode) {
|
|
|
|
|
|
|
|
$result.diff.prepared += "`n-[$($list[$idx])]`n+[$line]`n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(-not $check_mode) {
|
|
|
|
|
|
|
|
$list[$idx] = $line
|
|
|
|
$list[$idx] = $line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @{
|
|
|
|
return @{
|
|
|
|
line_removed = $line_removed
|
|
|
|
line_removed = $line_removed
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -153,19 +130,15 @@ Function Add-AliasesToEntry($list, $idx, $aliases) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($line -ne $list[$idx]){
|
|
|
|
if($line -ne $list[$idx]){
|
|
|
|
$result.changed = $true
|
|
|
|
$module.Result.changed = $true
|
|
|
|
if($diff_mode) {
|
|
|
|
|
|
|
|
$result.diff.prepared += "`n-[$($list[$idx])]`n+[$line]`n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(-not $check_mode) {
|
|
|
|
|
|
|
|
$list[$idx] = $line
|
|
|
|
$list[$idx] = $line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$hosts_lines = New-Object System.Collections.ArrayList
|
|
|
|
$hosts_lines = New-Object System.Collections.ArrayList
|
|
|
|
|
|
|
|
|
|
|
|
Get-Content -LiteralPath $hosts_file.FullName | ForEach-Object { $hosts_lines.Add($_) } | Out-Null
|
|
|
|
Get-Content -LiteralPath $hosts_file.FullName | ForEach-Object { $hosts_lines.Add($_) } | Out-Null
|
|
|
|
|
|
|
|
$module.Diff.before = ($hosts_lines -join "`n") + "`n"
|
|
|
|
|
|
|
|
|
|
|
|
if ($state -eq 'absent') {
|
|
|
|
if ($state -eq 'absent') {
|
|
|
|
# go through and remove canonical_name and ip
|
|
|
|
# go through and remove canonical_name and ip
|
|
|
@ -200,11 +173,9 @@ if($state -eq 'present') {
|
|
|
|
$aliases_to_remove = @()
|
|
|
|
$aliases_to_remove = @()
|
|
|
|
if($entry_parts.ip_address -eq $ip_address) {
|
|
|
|
if($entry_parts.ip_address -eq $ip_address) {
|
|
|
|
if($entry_parts.canonical_name -eq $canonical_name) {
|
|
|
|
if($entry_parts.canonical_name -eq $canonical_name) {
|
|
|
|
# don't need to worry about line being removed since canonical_name is present
|
|
|
|
|
|
|
|
$entry_idx = $idx
|
|
|
|
$entry_idx = $idx
|
|
|
|
|
|
|
|
|
|
|
|
if($action -eq 'set') {
|
|
|
|
if($action -eq 'set') {
|
|
|
|
# remove the entry's aliases that are not in $aliases
|
|
|
|
|
|
|
|
$aliases_to_remove = $entry_parts.aliases | Where-Object { $aliases -notcontains $_ }
|
|
|
|
$aliases_to_remove = $entry_parts.aliases | Where-Object { $aliases -notcontains $_ }
|
|
|
|
} elseif($action -eq 'remove') {
|
|
|
|
} elseif($action -eq 'remove') {
|
|
|
|
$aliases_to_remove = $aliases
|
|
|
|
$aliases_to_remove = $aliases
|
|
|
@ -220,41 +191,31 @@ if($state -eq 'present') {
|
|
|
|
# this is not the ip_address we are looking for
|
|
|
|
# this is not the ip_address we are looking for
|
|
|
|
if ($ip_address_type -eq $entry_parts.ip_type) {
|
|
|
|
if ($ip_address_type -eq $entry_parts.ip_type) {
|
|
|
|
if ($entry_parts.canonical_name -eq $canonical_name) {
|
|
|
|
if ($entry_parts.canonical_name -eq $canonical_name) {
|
|
|
|
# remove the entry
|
|
|
|
Remove-HostEntry -list $hosts_lines -idx $idx
|
|
|
|
if (Remove-HostEntry -list $hosts_lines -idx $idx){
|
|
|
|
|
|
|
|
# keep index correct if we removed the line
|
|
|
|
|
|
|
|
$idx = $idx - 1
|
|
|
|
$idx = $idx - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action -ne "set") {
|
|
|
|
if ($action -ne "set") {
|
|
|
|
# keep old aliases intact
|
|
|
|
# keep old aliases intact
|
|
|
|
$aliases_to_keep += $entry_parts.aliases | Where-Object { ($aliases + $aliases_to_keep + $canonical_name) -notcontains $_ }
|
|
|
|
$aliases_to_keep += $entry_parts.aliases | Where-Object { ($aliases + $aliases_to_keep + $canonical_name) -notcontains $_ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($action -eq "remove") {
|
|
|
|
} elseif ($action -eq "remove") {
|
|
|
|
# just remove canonical_name. user may want alias(es) mapped to this canonical name
|
|
|
|
|
|
|
|
$aliases_to_remove = $canonical_name
|
|
|
|
$aliases_to_remove = $canonical_name
|
|
|
|
} elseif ($aliases -contains $entry_parts.canonical_name) {
|
|
|
|
} elseif ($aliases -contains $entry_parts.canonical_name) {
|
|
|
|
# remove the entry
|
|
|
|
Remove-HostEntry -list $hosts_lines -idx $idx
|
|
|
|
if (Remove-HostEntry -list $hosts_lines -idx $idx) {
|
|
|
|
|
|
|
|
# keep index correct if we removed the line
|
|
|
|
|
|
|
|
$idx = $idx - 1
|
|
|
|
$idx = $idx - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action -eq "add") {
|
|
|
|
if ($action -eq "add") {
|
|
|
|
# keep old aliases intact
|
|
|
|
# keep old aliases intact
|
|
|
|
$aliases_to_keep += $entry_parts.aliases | Where-Object { ($aliases + $aliases_to_keep + $canonical_name) -notcontains $_ }
|
|
|
|
$aliases_to_keep += $entry_parts.aliases | Where-Object { ($aliases + $aliases_to_keep + $canonical_name) -notcontains $_ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
# ensure canonical_name and aliases removed from this entry
|
|
|
|
|
|
|
|
$aliases_to_remove = $aliases + $canonical_name
|
|
|
|
$aliases_to_remove = $aliases + $canonical_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
# Just ignore if the types don't match.
|
|
|
|
|
|
|
|
# TODO: Better ipv6 support. There is odd behavior for when an alias can be used for both ipv6 and ipv4
|
|
|
|
# TODO: Better ipv6 support. There is odd behavior for when an alias can be used for both ipv6 and ipv4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if($aliases_to_remove) {
|
|
|
|
if($aliases_to_remove) {
|
|
|
|
if((Remove-HostnamesFromEntry -list $hosts_lines -idx $idx -aliases $aliases_to_remove).line_removed) {
|
|
|
|
if((Remove-HostnamesFromEntry -list $hosts_lines -idx $idx -aliases $aliases_to_remove).line_removed) {
|
|
|
|
# keep index correct if we removed the line
|
|
|
|
|
|
|
|
$idx = $idx - 1
|
|
|
|
$idx = $idx - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -263,14 +224,11 @@ if($state -eq 'present') {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if($entry_idx -ge 0) {
|
|
|
|
if($entry_idx -ge 0) {
|
|
|
|
# we found the entry
|
|
|
|
|
|
|
|
$aliases_to_add = @()
|
|
|
|
$aliases_to_add = @()
|
|
|
|
$entry_parts = Get-HostEntryParts -line $hosts_lines[$entry_idx]
|
|
|
|
$entry_parts = Get-HostEntryParts -line $hosts_lines[$entry_idx]
|
|
|
|
if($action -eq 'remove') {
|
|
|
|
if($action -eq 'remove') {
|
|
|
|
# just preserve any previously removed aliases
|
|
|
|
|
|
|
|
$aliases_to_add = $aliases_to_keep | Where-Object { $entry_parts.aliases -notcontains $_ }
|
|
|
|
$aliases_to_add = $aliases_to_keep | Where-Object { $entry_parts.aliases -notcontains $_ }
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
# we want to add provided aliases and previously removed aliases that are not already in the list
|
|
|
|
|
|
|
|
$aliases_to_add = ($aliases + $aliases_to_keep) | Where-Object { $entry_parts.aliases -notcontains $_ }
|
|
|
|
$aliases_to_add = ($aliases + $aliases_to_keep) | Where-Object { $entry_parts.aliases -notcontains $_ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -291,8 +249,9 @@ if($state -eq 'present') {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( $result.changed -and -not $check_mode ) {
|
|
|
|
$module.Diff.after = ($hosts_lines -join "`n") + "`n"
|
|
|
|
|
|
|
|
if( $module.Result.changed -and -not $module.CheckMode ) {
|
|
|
|
Set-Content -LiteralPath $hosts_file.FullName -Value $hosts_lines
|
|
|
|
Set-Content -LiteralPath $hosts_file.FullName -Value $hosts_lines
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Exit-Json $result
|
|
|
|
$module.ExitJson()
|
|
|
|