How do I call Ansible Tower REST API endpoint to launch job_template for a job_template id?

9.2k Views Asked by At

I have Ansible Tower API version: 3.7.0 and Ansible version: 2.9.13.

The documentation (attached image for reference) says that it supports a POST to api/v2/job_templates/id/launch/ in order to launch job template.

However, when I call to the POST mentioned above I am getting the same response as I get via GET request to job_templates endpoint.

Expectation:

I want POST to api/v2/job_templates/id/launch/ return HTTP 201 status code and execute the job.

Actual:

I see 200 HTTP status code containing the response body same as GET api/v2/job_templates/id/launch and without the job_template being executed.

enter image description here

Also notice that the GET on browser for Get api/v2/job_templates/id/ does not include the POST as an allowed method. enter image description here

1

There are 1 best solutions below

4
U880D On

Based on your information provided I've performed a test under RHEL 7.9, Ansible Tower 3.7.3, Ansible v2.9.27.

First you need to get the correct ID for the job_template in question by calling in example

curl --silent -u "${ACCOUNT}:${PASSWORD}" https://${TOWER_URL}/api/v2/job_templates/ --write-out "\n%{http_code}\n" | jq .results
[
  {
    "id": 1,
    "type": "job_template",
    "url": "/api/v2/job_templates/1/",
    "related": {
      "created_by": "/api/v2/users/1/",
      "modified_by": "/api/v2/users/1/",
      "labels": "/api/v2/job_templates/1/labels/",
      "inventory": "/api/v2/inventories/1/",
      "project": "/api/v2/projects/1/",
      "organization": "/api/v2/organizations/1/",
...

You may filter it further jquery.

Then, with ID='1', a GET call

curl --silent -u "${ACCOUNT}:${PASSWORD}" -X GET https://${TOWER_URL}/api/v2/job_templates/${ID}/launch/ --write-out "\n%{http_code}\n" | jq .

Make a GET request to this resource to determine if the job_template can be launched and whether any passwords are required to launch the job_template ...

results into the correct information

{
  "can_start_without_user_input": false,
  "passwords_needed_to_start": [],
  "ask_scm_branch_on_launch": false,
  "ask_variables_on_launch": false,
  "ask_tags_on_launch": false,
  "ask_diff_mode_on_launch": false,
  "ask_skip_tags_on_launch": false,
  "ask_job_type_on_launch": false,
  "ask_limit_on_launch": true,
  "ask_verbosity_on_launch": false,
  "ask_inventory_on_launch": false,
  "ask_credential_on_launch": true,
  "survey_enabled": false,
  "variables_needed_to_start": [],
  "credential_needed_to_start": false,
  "inventory_needed_to_start": false,
  "job_template_data": {
    "name": "Test",
    "id": 1,
    "description": "Test"
  },
  "defaults": {
    "extra_vars": "---\ntarget_hosts: test",
    "diff_mode": false,
    "limit": "test",
    "job_tags": "check",
    "skip_tags": "",
    "job_type": "run",
    "verbosity": 0,
    "inventory": {
      "name": "Inventory",
      "id": 1
    },
    "scm_branch": ""
  }
}
200

as well a POST call

curl --silent -u "${ACCOUNT}:${PASSWORD}" -X POST https://${TOWER_URL}/api/v2/job_templates/${ID}/launch/ --write-out "\n%{http_code}\n" | jq .

Make a POST request to this resource to launch the job_template. If any passwords, inventory, or extra variables (extra_vars) are required, they must be passed via POST data, with extra_vars given as a YAML or JSON string and escaped parentheses ...

with result

{
  "job": 1,
  "ignored_fields": {},
  "id": 1,
  "type": "job",
  "url": "/api/v2/jobs/1/",
  ...
  "created": "2022-06-03T17:01:01.723657Z",
  "modified": "2022-06-03T17:01:01.779154Z",
  "name": "Test",
  "description": "Test",
  "job_type": "run",
  "inventory": 1,
  "project": 1,
...
}
201

Documentation