2

Trying to lunch a job workflow via REST API and passing extra variables for the playbook to consume, but returned body shows that provided variables are put in ignored_fields section.

Used POSTMAN and CURL to run the templates both returned the same result

CURL command

curl -X POST http://172.16.0.97/api/v2/job_templates/8/launch/ -H "Content-Type: application/json"  -H "Authorization: Bearer Je
gxwfQrdKQXoRUtNWtWFz62FX5bTy" -d "{\"extra_vars\": {\"vendor\":\"juniper\"}}"

Returned body

{"job":34,"ignored_fields":{"extra_vars":{"vendor":"juniper"}},"id":34,"type":"job","url":"/api/v2/jobs/34/","related":{"created_by":"/api/v2/users/1/","modified_by":"/api/v2/users/1/","labels":"/api/v2/jobs/34/labels/","inventory":"/api/v2/inventories/1/","project":"/api/v2/projects/7/","extra_credentials":"/api/v2/jobs/34/extra_credentials/","credentials":"/api/v2/jobs/34/credentials/","unified_job_template":"/api/v2/job_templates/8/","stdout":"/api/v2/jobs/34/stdout/","job_events":"/api/v2/jobs/34/job_events/","job_host_summaries":"/api/v2/jobs/34/job_host_summaries/","activity_stream":"/api/v2/jobs/34/activity_stream/","notifications":"/api/v2/jobs/34/notifications/","job_template":"/api/v2/job_templates/8/","cancel":"/api/v2/jobs/34/cancel/","create_schedule":"/api/v2/jobs/34/create_schedule/","relaunch":"/api/v2/jobs/34/relaunch/"},"summary_fields":{"inventory":{"id":1,"name":"Demo Inventory","description":"","has_active_failures":true,"total_hosts":1,"hosts_with_active_failures":1,"total_groups":0,"groups_with_active_failures":0,"has_inventory_sources":false,"total_inventory_sources":0,"inventory_sources_with_failures":0,"organization_id":1,"kind":""},"project":{"id":7,"name":"Cox-Phase3","description":"","status":"successful","scm_type":"git"},"job_template":{"id":8,"name":"Port Flap","description":""},"unified_job_template":{"id":8,"name":"Port Flap","description":"","unified_job_type":"job"},"created_by":{"id":1,"username":"admin","first_name":"","last_name":""},"modified_by":{"id":1,"username":"admin","first_name":"","last_name":""},"user_capabilities":{"delete":true,"start":true},"labels":{"count":0,"results":[]},"extra_credentials":[],"credentials":[]},"created":"2019-05-14T09:43:16.115516Z","modified":"2019-05-14T09:43:16.177517Z","name":"Port Flap","description":"","job_type":"run","inventory":1,"project":7,"playbook":"main.yml","forks":0,"limit":"","verbosity":1,"extra_vars":"{}","job_tags":"","force_handlers":false,"skip_tags":"","start_at_task":"","timeout":0,"use_fact_cache":false,"unified_job_template":8,"launch_type":"manual","status":"pending","failed":false,"started":null,"finished":null,"elapsed":0.0,"job_args":"","job_cwd":"","job_env":{},"job_explanation":"","execution_node":"","controller_node":"","result_traceback":"","event_processing_finished":false,"job_template":8,"passwords_needed_to_start":[],"ask_diff_mode_on_launch":false,"ask_variables_on_launch":false,"ask_limit_on_launch":false,"ask_tags_on_launch":false,"ask_skip_tags_on_launch":false,"ask_job_type_on_launch":false,"ask_verbosity_on_launch":false,"ask_inventory_on_launch":false,"ask_credential_on_launch":false,"allow_simultaneous":false,"artifacts":{},"scm_revision":"","instance_group":null,"diff_mode":false,"job_slice_number":0,"job_slice_count":1,"credential":null,"vault_credential":null}
gh0st
  • 402
  • 5
  • 13
  • You failed to specify your Tower version, but [3.0 has made `extra_vars` more strict](https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#ug-jobtemplates-extravars); are those conditions applicable to you (3.0, enabled survey, and/or ask on launch = true)? – mdaniel May 14 '19 at 16:03
  • Separately, while you didn't ask this, your life will be considerably better if you use the distinction between single and double quotes in shell, as your `curl` command would be less verbose with `-d '{"extra_vars": {"vendor":"juniper"}}'` – mdaniel May 14 '19 at 16:06
  • @MatthewLDaniel - Thanks for the comment. To be honest I am using Ansible AWX not the tower and I am running on the latest version possible. Regarding single and double quotes this was my third attempt to call the API. I was using different formats before that. – gh0st May 14 '19 at 16:14
  • 2
    Well, Tower is just AWX-for-money, so if you're on the latest AWX then you are on 4, which likely is affected by that change. You skipped over the other two parts: do you have an enabled survey for that template, and/or do you have `ask_variables_on_launch=True` like the docs say? – mdaniel May 14 '19 at 16:22
  • 1
    @MatthewLDaniel - After turning on the `ask_variables_on_launch=True` it worked. Please submit the answer and I will mark it as the correct. Thanks for helping! – gh0st May 14 '19 at 20:26
  • You're very kind, and I'm glad it was something simple :-) – mdaniel May 15 '19 at 01:44
  • *Trying to lunch* been there, done that. – greybeard Oct 18 '22 at 08:55

3 Answers3

11

According to the fine manual, AWX (and thus Tower) version 3.0 and greater has made extra_vars more strict: https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#ug-jobtemplates-extravars

If you are running a version greater than 3.0, you will need to either turn on playbook survey or set ask_variables_on_launch=True for that template

mdaniel
  • 31,240
  • 5
  • 55
  • 58
0

In my case, I'm using curl -L ... and the payload got lost after the redirect. Be sure to double check that if you find the extra_vars still gets ignored after ensuring ask_variables_on_launch=True.

jellycsc
  • 10,904
  • 2
  • 15
  • 32
  • 1
    I was getting a redirect because the url did not have a trailing /. (should be `TEMPLATE/launch/` not `TEMPLATE/launch`). Once I added the trailing slash, the redirect went away. – Lincoln D Feb 15 '22 at 16:34
  • @LincolnD Damned if that wasn't my problem. Thanks. – Jack Feb 24 '23 at 20:27
0

Tangentially related to the API when utilizing the AWX and Tower CLI I ran into a similar issue of variables not being taken when launching jobs. The solution was that on the Job Template in Tower the "Prompt on Launch" setting needed to checked for the variable to pass through. So much time wasted on such a simple miss.