|
26 | 26 | import time |
27 | 27 | import sys |
28 | 28 |
|
| 29 | +def create_payload(extra_vars, jobTags, credentials): |
| 30 | + body = {} |
| 31 | + body['extra_vars'] = extra_vars |
| 32 | + body['job_tags'] = ",".join(jobTags) |
| 33 | + body['credentials'] = credentials |
| 34 | + body_as_string = json.dumps(body) |
| 35 | + print "Body returned from create_payload() is %s" % body_as_string |
| 36 | + return body_as_string |
| 37 | + |
29 | 38 | if not ansibletower: |
30 | 39 | print("Ansible Tower Server must be provided") |
31 | 40 | sys.exit(1) |
|
40 | 49 |
|
41 | 50 | if not isWorkflow: |
42 | 51 | api_url = '/api/v2/job_templates/%s/launch/' % job_template_id |
43 | | - body = { "extra_vars": extra_vars} |
44 | | - response = request.post(api_url,body= json.dumps(body),contentType="application/json",headers=headers) |
| 52 | + # body = { "extra_vars": extra_vars} |
| 53 | + body_as_string = create_payload(extra_vars, jobTags, credentials) |
| 54 | + response = request.post(api_url,body=body_as_string,contentType="application/json",headers=headers) |
45 | 55 | result = json.loads(response.response) |
46 | 56 | # Check the response status code to make sure the request was not successful. |
47 | | - |
| 57 | + |
48 | 58 | if not response.isSuccessful(): |
49 | 59 | print("Failed to run the job template. Server return [%s], with content [%s]" % (response.status, response.response)) |
50 | 60 | sys.exit(1) |
51 | | - |
| 61 | + |
52 | 62 | job_id= result["id"] |
53 | | - |
| 63 | + |
54 | 64 | print("```") |
55 | 65 | print(">>> Job template launched with job id " + str(job_id)) |
56 | 66 | print("```") |
57 | 67 | print("\n") |
58 | | - |
| 68 | + |
59 | 69 | num_tries = 0 |
60 | 70 | api_url = '/api/v2/jobs/%s/' % job_id |
61 | 71 |
|
62 | | - |
| 72 | + |
63 | 73 | if max_retries: |
64 | 74 | while num_tries < int(max_retries): |
65 | 75 | # Define the URL for the Ansible Tower API endpoint to launch a job. |
|
75 | 85 | time.sleep(float(wait_interval)) |
76 | 86 | else: |
77 | 87 | raise Exception("Failed !. Server return [%s], with content [%s]" % (response.status, response.response)) |
78 | | - |
| 88 | + |
79 | 89 | if num_tries == int(max_retries): |
80 | 90 | # maximum number of tries reached, handle the error |
81 | 91 | raise Exception("Error: maximum number of tries reached") |
82 | 92 | else: |
83 | | - # task completed |
| 93 | + # task completed |
84 | 94 | print("```") |
85 | 95 | print(">>> Task completed after " + str(num_tries) + " tries with status "+ status) |
86 | 96 | print("```") |
|
93 | 103 | print("* [>>> Job %s Link](%s/#/jobs/%s) <<<" % (str(job_id), ansibletower['url'], str(job_id))) |
94 | 104 | print("\n") |
95 | 105 | result= job_output |
96 | | - |
| 106 | + |
97 | 107 | else: |
98 | 108 | while True: |
99 | 109 | # Define the URL for the Ansible Tower API endpoint to launch a job. |
|
122 | 132 | result= job_output |
123 | 133 | else: |
124 | 134 | api_url = '/api/v2/workflow_job_templates/%s/launch/' % job_template_id |
125 | | - body = { "extra_vars": extra_vars} |
126 | | - response = request.post(api_url,body= json.dumps(body),contentType="application/json",headers=headers) |
| 135 | +# body = { "extra_vars": extra_vars} |
| 136 | + body_as_string = create_payload(extra_vars, jobTags, credentials) |
| 137 | + response = request.post(api_url,body=body_as_string,contentType="application/json",headers=headers) |
127 | 138 | result = json.loads(response.response) |
128 | 139 | # Check the response status code to make sure the request was not successful. |
129 | | - |
| 140 | + |
130 | 141 | if not response.isSuccessful(): |
131 | 142 | print("Failed to run the workflow job template. Server return [%s], with content [%s]" % (response.status, response.response)) |
132 | 143 | sys.exit(1) |
133 | | - |
| 144 | + |
134 | 145 | job_id= result["id"] |
135 | | - |
| 146 | + |
136 | 147 | print("```") |
137 | 148 | print(">>> Workflow job template launched with job id " + str(job_id)) |
138 | 149 | print("```") |
139 | 150 | print("\n") |
140 | | - |
| 151 | + |
141 | 152 | num_tries = 0 |
142 | 153 | api_url = '/api/v2/workflow_jobs//%s/' % job_id |
143 | 154 | if max_retries: |
|
155 | 166 | time.sleep(float(wait_interval)) |
156 | 167 | else: |
157 | 168 | raise Exception("Failed !. Server return [%s], with content [%s]" % (response.status, response.response)) |
158 | | - |
| 169 | + |
159 | 170 | if num_tries == int(max_retries): |
160 | 171 | # maximum number of tries reached, handle the error |
161 | 172 | raise Exception("Error: maximum number of tries reached") |
162 | 173 | else: |
163 | | - # task completed |
| 174 | + # task completed |
164 | 175 | print("```") |
165 | 176 | print(">>> Task completed after " + str(num_tries) + " tries with status "+ status) |
166 | 177 | print("```") |
|
198 | 209 | print("\n") # end markdown code block |
199 | 210 | print("* [>>> Job %s Link](%s/#/jobs/workflow/%s/output) <<<" % (str(job_id), ansibletower['url'], str(job_id))) |
200 | 211 | print("\n") |
201 | | - result= job_output |
| 212 | + result= job_output |
202 | 213 |
|
203 | 214 | if stopOnFailure and not status == 'successful': |
204 | 215 | raise Exception("Job status is "+ status) |
205 | | - |
206 | | - |
|
0 commit comments