0

I'm trying to run AWX job template from REST API using Python. It is working fine when just running job template without any arguments, but I will need to specify the target hosts in the execution. When using get method I can see there is a 'limit' argument. Unfortunately when I run the code with python script.py job_id I'm getting the ignored field for limit: b'{"job":2133,"ignored_fields":{"limit":"server-PRC-05"},"id":2133,"type":"job","url":" etc.. I wasn't able to find working example for this. Please find my python code below:

import sys
import requests
import json
templateid=sys.argv[1]
from requests.auth import HTTPBasicAuth
headers = {'content-type': 'application/json'}
data_new = {'limit': 'server-PRC-05'}
res = requests.post('http://awx_host/api/v2/job_templates/'+templateid+'/launch/', 
verify=False, auth=HTTPBasicAuth('user', 'password'), data=json.dumps(data_new), headers=headers)
print (res.content)
PawelN
  • 33
  • 1
  • 4

2 Answers2

2

API /api/v2/job_templates/{id}/launch/ received just extra-vars as data, add limit var as extra-vars.

Additional strict extra_vars validation was added in Ansible Tower 3.0.0. extra_vars passed to the job launch API are only honored if one of the following is true:

  1. They correspond to variables in an enabled survey
  2. ask_variables_on_launch is set to True

https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#extra-variables

Then set ask_variables_on_launch=True in that template.

gary lopez
  • 1,823
  • 7
  • 15
1

You need enable ask_limit_on_launch parameter and then pass limit as list (array) in the payload.

Your may look like this ->

data_new = {'limit': ['server-PRC-05']}

Observe limit is must list (array)

to enable ask_limit_on_launch refer link Ansible tower API: pass inventory for a job in a POST payload