Skip to content

Instantly share code, notes, and snippets.

@juanesech
Last active June 9, 2020 19:20
Show Gist options
  • Save juanesech/2ba3b5b5f4a19d8f8020e0b1237954d9 to your computer and use it in GitHub Desktop.
Save juanesech/2ba3b5b5f4a19d8f8020e0b1237954d9 to your computer and use it in GitHub Desktop.
$uname = 'USERNAME'
$passwd = 'PASSWORD'
$TowerApiUrl = 'https://<REPLACE_WITH_TOWER_URL>/api/v2'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Authorization = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$uname`:$passwd"))
$MeUri = $TowerApiUrl + '/me/'
$MeResult = Invoke-RestMethod -Uri $MeUri -Headers @{ "Authorization" = "Basic $Authorization" ; "Content-Type" = 'application/json'} -ErrorAction Stop
### Logging in to Tower...
$PATUri = $TowerApiUrl + '/users/' + $($MeResult.Results.id) + '/personal_tokens/'
$Tokens = Invoke-RestMethod -Uri $PATUri -Method POST -Headers @{ "Authorization" = "Basic $Authorization" ; "Content-Type" = 'application/json'} -ContentType "application/json"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$tokenId = $Tokens.id
$headers.Add("Authorization", "Bearer " + $Tokens.token);
$templateNumber = 2399
$bodyParameters = '' # Any parameters that the Template may require
$urlTemplate = $TowerApiUrl + "/job_templates/$templateNumber/launch/";
$ansibleResponse = Invoke-RestMethod -Uri $urlTemplate -Method Post -Body $bodyParameters -Headers $headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$tokenId = $Tokens.id
$headers.Add("Authorization", "Bearer " + $Tokens.token);
$urlTemplate = $TowerApiUrl + "/jobs/$($ansibleResponse.id)/";
$ansibleResponse = Invoke-RestMethod -Uri $urlTemplate -Method Get -Headers $headers
$ansibleResponse.status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment