With Ansible tower or the open-source variant AWX you can create Job templates and workflows. Starting these job templates could be done using the GUI, a Schedule or using the cool REST API. Because I like scripting in PowerShell I used the Ansible Tower PowerShell module which is already available in the following Github Repository: Github Repo
In the following example I use the: Connect-AnsibleTower, Get-AnsibleJobTemplate, Invoke-AnsibleRequest from the module created by gpduck. Thanks!
It was a little search how to parse the Ansible job output to something we could re-use in PowerShell. I just print the msg var in my playbook and read this one from the job_events rest API call.
If you have a better way to do this, just commend below :).
Printing a variable in Ansible:
1 2 3 4 5 |
- name: get content win_shell: 'type c:\sometextfile.txt' register: content - name: write content debug: msg="{{ content.stdout_lines }}" |
Get job template output using the API (sample function):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#In this example i use a limit filter to limit the collection #Connect to Ansible API Connect-AnsibleTower -TowerUrl "http://TOWERURI" -Credential $ACred #The sample function Function InvokeAWXJob { Param( [Parameter(Mandatory=$true)] $JobTemplate, [Parameter(Mandatory=$true)] $PrintVar, [Parameter(Mandatory=$true)] $SystemName ) $myObject = [PSCustomObject]@{ limit = "$SystemName" } $JBody = $myObject | ConvertTo-Json $jobId = Get-AnsibleJobTemplate -Name $JobTemplate $c = Invoke-AnsibleRequest -fullpath "/api/v2/job_templates/$($jobid.id)/launch/" -Method POST -Body $JBody $a = Wait-Ansiblejob -id $c.id -interval 4 if($a.status -eq "failed"){ Write-host "Failed to start" } $k = Invoke-AnsibleRequest -fullPath "/api/v2/jobs/$($c.id)/job_events/?search=$($printvar)" If(!$k){ write-host "No joboutput found" } $jobResult = $k.results.event_data.res.$($PrintVar) $jobResult } #Using the function $Ajob = InvokeAWXJob -JobTemplate "SampleJob" -PrintVar "msg" -SystemName $Ost $Ajob |