|
| 1 | +#!powershell |
| 2 | + |
| 3 | +#AnsibleRequires -CSharpUtil Ansible.Basic |
| 4 | + |
| 5 | +### Input parameters |
| 6 | +$spec = @{ |
| 7 | + options = @{ |
| 8 | + state = @{ type = "str"; choices = "absent", "present"; default = "present" } |
| 9 | + command = @{ type = "str"; required = $true } |
| 10 | + arguments = @{ type = "dict"; required = $false; default = @{} } |
| 11 | + } |
| 12 | + supports_check_mode = $true |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +### Module initilization |
| 17 | +$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 18 | + |
| 19 | + |
| 20 | +### Make use of input parameters |
| 21 | +$Changed = $false |
| 22 | +$State = $module.Params.state |
| 23 | +$Command = $module.Params.command |
| 24 | +$Arguments = $module.Params.arguments |
| 25 | + |
| 26 | +# Sanetize $Arguments, prepend "-" to get "<COMMAND> -<FLAG> <VALUE>" |
| 27 | +$TmpArguments = @{} |
| 28 | +foreach ($Argument in $Arguments.Keys) { |
| 29 | + if (-Not $Argument.StartsWith("-")) { |
| 30 | + $TmpArguments."-$($Argument)" = $Arguments.$Argument |
| 31 | + } |
| 32 | +} |
| 33 | +$Arguments = $TmpArguments |
| 34 | + |
| 35 | + |
| 36 | +### Main code |
| 37 | +# Check if IfW is installed |
| 38 | +if (-Not (Get-Command | Where-Object -Property Name -EQ "Show-IcingaRegisteredBackgroundDaemons")) { |
| 39 | + throw "Necessary command 'Show-IcingaRegisteredBackgroundDaemons' was not found. Is IfW installed?" |
| 40 | +} |
| 41 | + |
| 42 | +# Check that $Command is valid and available |
| 43 | +if (-Not (Get-Command | Where-Object -Property Name -EQ $Command)) { |
| 44 | + throw "Necessary command '$($Command)' was not found." |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +# Check if BackgroundDaemon for given command exists |
| 49 | +function BackgroundDaemon-Exists () { |
| 50 | + param( |
| 51 | + [String]$Command |
| 52 | + ); |
| 53 | + |
| 54 | + $Exists = (Show-IcingaRegisteredBackgroundDaemons) -Contains $Command |
| 55 | + return $Exists |
| 56 | +} |
| 57 | + |
| 58 | +# Check if given command arguments are equal to existing command arguments |
| 59 | +function ArgumentsAreEqual () { |
| 60 | + param( |
| 61 | + $Arguments, |
| 62 | + $ExistingArguments |
| 63 | + ); |
| 64 | + |
| 65 | + $ArgumentsAreEqual = $true |
| 66 | + foreach ($Key in $Arguments.keys) { |
| 67 | + if ($Arguments.$Key -NE $ExistingArguments.$Key) { |
| 68 | + $ArgumentsAreEqual = $false |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + return $ArgumentsAreEqual |
| 73 | +} |
| 74 | + |
| 75 | +# Get existing arguments for given command |
| 76 | +function Get-ExistingArguments () { |
| 77 | + param( |
| 78 | + [String]$Command |
| 79 | + ); |
| 80 | + |
| 81 | + $Arguments = (Read-IcingaPowerShellConfig).BackgroundDaemon.EnabledDaemons."$($Command)".Arguments |
| 82 | + |
| 83 | + return $Arguments |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +$CommandIsRegistered = BackgroundDaemon-Exists -Command $Command |
| 89 | +$ExistingArguments = Get-ExistingArguments -Command $Command |
| 90 | +$ArgumentsAreEqual = ArgumentsAreEqual -Arguments $Arguments -ExistingArguments $ExistingArguments |
| 91 | + |
| 92 | + |
| 93 | +# Update if needed |
| 94 | +if ($State -EQ "absent" -And $CommandIsRegistered) { |
| 95 | + if (-Not $module.CheckMode) { |
| 96 | + Unregister-IcingaBackgroundDaemon ` |
| 97 | + -BackgroundDaemon $Command | Out-Null |
| 98 | + } |
| 99 | + $Changed = $true |
| 100 | + |
| 101 | +} elseif ($State -EQ "present" -And (-Not $CommandIsRegistered -Or -Not $ArgumentsAreEqual)) { |
| 102 | + if (-Not $module.CheckMode) { |
| 103 | + Register-IcingaBackgroundDaemon ` |
| 104 | + -Command $Command ` |
| 105 | + -Arguments $Arguments | Out-Null |
| 106 | + } |
| 107 | + $Changed = $true |
| 108 | +} |
| 109 | + |
| 110 | +$Before = @{ |
| 111 | + command = (&{ if ($CommandIsRegistered) { $Command } else { $null } } ) |
| 112 | + arguments = (&{ if ($CommandIsRegistered) { $ExistingArguments } else { $null } } ) |
| 113 | +} |
| 114 | +$After = @{ |
| 115 | + command = (&{ if ($State -EQ "present") { $Command } else { $null } } ) |
| 116 | + arguments = (&{ if ($State -EQ "present") { $Arguments } else { $null } } ) |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +### Module return |
| 122 | +$module.Result.before = $Before |
| 123 | +$module.Result.after = $After |
| 124 | +$module.Result.changed = $Changed |
| 125 | +$module.ExitJson() |
0 commit comments