Created
October 4, 2015 14:23
-
-
Save trondhindenes/d7c28914e971963e066a to your computer and use it in GitHub Desktop.
Example of a module supporting using a scheduled job with support for alternate credentials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!powershell | |
# This file is part of Ansible. | |
# | |
# Copyright 2015, Trond Hindenes <[email protected]> | |
# | |
# Ansible is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# Ansible is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | |
# WANT_JSON | |
# POWERSHELL_COMMON | |
$params = Parse-Args $args; | |
$runas_user = Get-AnsibleParam -obj $params -name runas_user | |
if ($runas_user -ne $null) | |
{ | |
$runas_pass = Get-AnsibleParam -obj $params -name runas_pass | convertto-securestring -asplaintext -force | |
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $runas_user, $runas_pass | |
RunAsScheduledJob -credential $cred | |
} | |
Else | |
{ | |
RunAsScheduledJob | |
} | |
if ($IsScheduledJob -eq $true) | |
{ | |
$params = Parse-Args $args; | |
$result = New-Object psobject @{ | |
changed = $false | |
} | |
$ShouldFail = Get-AnsibleParam -obj $params -name "should_fail"| ConvertTo-Bool | |
$Message = Get-AnsibleParam -obj $params -name "message" | |
set-attr -obj $result -name "message" -value $message | |
set-attr -obj $result -name "user_name" -value $env:username | |
if ($ShouldFail -eq $false) | |
{ | |
exit-json -obj $result | |
} | |
if ($ShouldFail -eq $true) | |
{ | |
fail-json -obj $result | |
} | |
} | |
Else | |
{ | |
$job = GetScheduledJobResult | |
if ($job.result -eq "Success") | |
{ | |
exit-json -obj $job.obj | |
} | |
Else | |
{ | |
fail-json -obj $job.obj | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment