0

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

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
saad bin sami
  • 364
  • 4
  • 11
  • Since almost all debugging information is missing and the issue is not reproducible, can you provide information on: How do you construct the REST API calls? What is full JSON response of your call? – U880D Jun 03 '22 at 17:25
  • I was using the Postman to call the job_template launch endpoint. – saad bin sami Jun 03 '22 at 18:14

1 Answers1

3

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

U880D
  • 8,601
  • 6
  • 24
  • 40
  • I have the correct job_template Id taken from the URL, when you click on the job template. In addition to that, when i did a POST to api/user to create a user, it returned me the list of users instead of the creation of user, so it looked like that the POST in my ansible tower is overridden by the GET. Also the response, that you had pasted for GET request, this is what i am getting in result of a POST request to launch job_template endpoint. – saad bin sami Jun 03 '22 at 18:16
  • 1
    @saadbinsami, regarding "_the response, that you had pasted for `GET` request, this is what I am getting in result of a `POST` request to launch `job_template` endpoint_", as you can see from my example this isn't reproducible for me. Therefore it is recommended to provide debugging details, how do you construct the REST API calls together with the full JSON response by editing your question. – U880D Jun 03 '22 at 18:25
  • Sorry my bad, i was not calling the api, via the Https, instead i was calling it via Http(because the machine on which we have ansible installed is inside the network) protocol in postman, and this result in a weird response, i.e i was getting a GET response in request of a POST. – saad bin sami Jun 03 '22 at 19:08
  • @saadbinsami this is why we usually ask for [Minimal complete and verifiable examples](/help/mcve) which you can provide at start for your next question (and might even solve the problem while you craft it and before you ask). You were not getting a weird response, simply a 302 redirect to your https endpoint and the redirect is made via GET. – Zeitounator Jun 04 '22 at 06:34