Creating a Job in Ansible Tower via the REST API

Now that we can use the REST API to list inventory, it is not a big stretch to decide we want to kick off Jobs, too. Here it is in a nutshell, and some related operations for working with jobs and templates.

To List Templates:

curl -s  -k  -u $CREDS https://$TOWER_HOST/api/v2/job_templates/ | jq '.results | .[] | .name '
"MOC Stack  Teardown Production"
"MOC Stack Update Causal"
"RDU OpenShift Build"
"RDU OpenShift Tear Down"
"RDU Stack Provision"
"RDU Stack Subscribe"
"Stack Tear Down"

To Select the launch URL for a Template by name:

 curl -s  -k  -X POST -u $CREDS https://$TOWER_HOST/api/v2/job_templates/11/launch/ | jq '.url'

To Launch it (and get the resulting job URL:

curl -s  -k  -X POST -u $CREDS https://$TOWER_HOST/api/v2/job_templates/11/launch/ | jq '.url'

To figure out what happened on a job:

 curl -s  -k  -u $CREDS https://$TOWER_HOST/api/v2/jobs/1015/ | jq '.job_explanation'
"Previous Task Failed: {\"job_type\": \"project_update\", \"job_name\": \"Rippowam\", \"job_id\": \"1016\"}"

To Find the URL of Template used to launch a job:

curl -s  -k  -u $CREDS https://$TOWER_HOST/api/v2/jobs/1021/ | jq '.related | .job_template'

To list the names of templates used to kick off jobs:

curl -s  -k  -u $CREDS https://$TOWER_HOST/api/v2/jobs/ | jq '.results | .[]| .summary_fields| .job_template | .name'

To Kick Off a Job again:

export RELAUNCH_URL=$( curl -s  -k  -u $CREDS https://$TOWER_HOST/api/v2/jobs/1021/ | jq -r '.related | .relaunch' )
curl -s  -k  -X POST -u $CREDS https://$TOWER_HOST$RELAUNCH_URL

Note the -r option to curl to strip the quotes, and the lack of a / between $TOWER_HOST and $RELAUNCH_URL.

Leave a Reply

Your email address will not be published. Required fields are marked *