2018-07-07 06:18:12 +03:00
# Define parameters
[ cmdletbinding ( ) ]
Param (
[ Alias ( " k " ) ]
[ switch ] $insecure = $false ,
[ Alias ( " h " ) ]
[ switch ] $help ,
[ Alias ( " s " ) ]
[ string ] $tower_url ,
[ Alias ( " c " ) ]
[ string ] $host_config_key ,
[ Alias ( " t " ) ]
[ string ] $job_template_id ,
[ Alias ( " e " ) ]
[ string ] $extra_vars
2015-11-05 18:14:30 +03:00
)
2018-07-07 06:18:12 +03:00
# Initialize variables
2015-11-05 18:14:30 +03:00
Set-StrictMode -Version 2
$ErrorActionPreference = " Stop "
2018-07-07 06:18:12 +03:00
$retry_attempts = 10
$attempt = 0
$usage = @"
Request server configuration from Ansible Tower
Usage :
Execution using positional parameters :
$ ( $MyInvocation . MyCommand . Name ) https : / / example . towerhost . net 44d7507f2ead49af5fca80aa18fd24bc 38
Ignore self-signed certificates using named parameters :
$ ( $MyInvocation . MyCommand . Name ) -k -s https : / / example . towerhost . local -c 44d7507f2ead49af5fca80aa18fd24bc -t 38
Execution using optional extra_vars :
$ ( $MyInvocation . MyCommand . Name ) https : / / example . towerhost . net 44d7507f2ead49af5fca80aa18fd24bc 38 '{ key: value, dict: { key: value }}'
Options :
-help , -h Show this message
-tower_url , -s Tower server ( e . g . https : / / < server address > [ : server port ] ) ( required )
-insecure , -k Allow insecure SSL connections and transfers
-host_config_key , -c < host config key > Host config key ( required )
-job_template_id , -t < job template id > Job template ID ( required )
-extra_vars , -e [ < extra vars > ] Extra variables
" @
2015-11-05 18:14:30 +03:00
2018-07-07 06:18:12 +03:00
# Validate required arguments
If ( -not $tower_url -or -not $host_config_key -or -not $job_template_id -or $help ) {
Write-Host $usage
2015-11-05 18:14:30 +03:00
Exit 1
}
2018-07-07 06:18:12 +03:00
# Create Invoke-WebRequest JSON data hash tables
If ( -not $extra_vars ) {
2018-03-21 15:22:08 +03:00
$data = @ {
host_config_key = $host_config_key
}
} Else {
$data = @ {
host_config_key = $host_config_key
extra_vars = $extra_vars
}
2015-11-05 18:14:30 +03:00
}
2018-07-07 06:18:12 +03:00
# Success on any 2xx status received, failure on only 404 status received, retry any other status every min for up to 10 min
2015-11-05 18:14:30 +03:00
While ( $attempt -lt $retry_attempts ) {
Try {
2018-07-07 06:18:12 +03:00
If ( $insecure ) {
[ System.Net.ServicePointManager ] :: ServerCertificateValidationCallback = { $true }
}
2018-03-21 15:22:08 +03:00
$resp = Invoke-WebRequest -ContentType application / json -Method POST -Body ( ConvertTo-Json $data ) -Uri $tower_url / api / v2 / job_templates / $job_template_id / callback /
If ( $resp . StatusCode -match '^2[0-9]+$' ) {
2015-11-05 18:14:30 +03:00
Exit 0
}
}
2018-07-07 06:18:12 +03:00
Catch [ System.Security.Authentication.AuthenticationException ] {
Write-Host $_
Exit 1
}
2015-11-05 18:14:30 +03:00
Catch {
$ex = $_
$attempt + +
2018-07-07 06:18:12 +03:00
If ( $ ( [ int ] $ex . Exception . Response . StatusCode ) -eq 404 ) {
Write-Host " $( [ int ] $ex . Exception . Response . StatusCode ) received... encountered problem, halting "
Exit 1
}
2015-11-05 18:14:30 +03:00
Write-Host " $( [ int ] $ex . Exception . Response . StatusCode ) received... retrying in 1 minute (Attempt $attempt ) "
}
2018-07-07 06:18:12 +03:00
Finally {
If ( $insecure ) {
$sp = [ System.Net.ServicePointManager ] :: FindServicePoint ( $tower_url )
$sp . CloseConnectionGroup ( " " ) > $null
[ System.Net.ServicePointManager ] :: ServerCertificateValidationCallback = $null
}
}
2015-11-05 18:14:30 +03:00
Start-Sleep -Seconds 60
}
2018-03-21 15:22:08 +03:00
Exit 1