| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2021, Phillipe Smith <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: rundeck_job_run |
| 11 |
short_description: Run a Rundeck job |
| 12 |
description: |
| 13 |
- This module runs a Rundeck job specified by ID. |
| 14 |
author: "Phillipe Smith (@phsmith)" |
| 15 |
version_added: 3.8.0 |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: none |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
job_id: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- The job unique ID. |
| 26 |
required: true |
| 27 |
job_options: |
| 28 |
type: dict |
| 29 |
description: |
| 30 |
- The job options for the steps. |
| 31 |
- Numeric values must be quoted. |
| 32 |
filter_nodes: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Filter the nodes where the jobs must run. |
| 36 |
- See U(https://docs.rundeck.com/docs/manual/11-node-filters.html#node-filter-syntax). |
| 37 |
run_at_time: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- Schedule the job execution to run at specific date and time. |
| 41 |
- ISO-8601 date and time format like V(2021-10-05T15:45:00-03:00). |
| 42 |
loglevel: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Log level configuration. |
| 46 |
choices: [debug, verbose, info, warn, error] |
| 47 |
default: info |
| 48 |
wait_execution: |
| 49 |
type: bool |
| 50 |
description: |
| 51 |
- Wait until the job finished the execution. |
| 52 |
default: true |
| 53 |
wait_execution_delay: |
| 54 |
type: int |
| 55 |
description: |
| 56 |
- Delay, in seconds, between job execution status check requests. |
| 57 |
default: 5 |
| 58 |
wait_execution_timeout: |
| 59 |
type: int |
| 60 |
description: |
| 61 |
- Job execution wait timeout in seconds. |
| 62 |
- If the timeout is reached, the job is aborted. |
| 63 |
- Keep in mind that there is a sleep based on O(wait_execution_delay) after each job status check. |
| 64 |
default: 120 |
| 65 |
abort_on_timeout: |
| 66 |
type: bool |
| 67 |
description: |
| 68 |
- Send a job abort request if exceeded the O(wait_execution_timeout) specified. |
| 69 |
default: false |
| 70 |
extends_documentation_fragment: |
| 71 |
- community.general._rundeck |
| 72 |
- ansible.builtin.url |
| 73 |
- community.general._attributes |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Run a Rundeck job |
| 78 |
community.general.rundeck_job_run: |
| 79 |
url: "https://rundeck.example.org" |
| 80 |
api_version: 39 |
| 81 |
api_token: "mytoken" |
| 82 |
job_id: "xxxxxxxxxxxxxxxxx" |
| 83 |
register: rundeck_job_run |
| 84 |
|
| 85 |
- name: Show execution info |
| 86 |
ansible.builtin.debug: |
| 87 |
var: rundeck_job_run.execution_info |
| 88 |
|
| 89 |
- name: Run a Rundeck job with options |
| 90 |
community.general.rundeck_job_run: |
| 91 |
url: "https://rundeck.example.org" |
| 92 |
api_version: 39 |
| 93 |
api_token: "mytoken" |
| 94 |
job_id: "xxxxxxxxxxxxxxxxx" |
| 95 |
job_options: |
| 96 |
option_1: "value_1" |
| 97 |
option_2: "value_3" |
| 98 |
option_3: "value_3" |
| 99 |
register: rundeck_job_run |
| 100 |
|
| 101 |
- name: Run a Rundeck job with timeout, delay between status check and abort on timeout |
| 102 |
community.general.rundeck_job_run: |
| 103 |
url: "https://rundeck.example.org" |
| 104 |
api_version: 39 |
| 105 |
api_token: "mytoken" |
| 106 |
job_id: "xxxxxxxxxxxxxxxxx" |
| 107 |
wait_execution_timeout: 30 |
| 108 |
wait_execution_delay: 10 |
| 109 |
abort_on_timeout: true |
| 110 |
register: rundeck_job_run |
| 111 |
|
| 112 |
- name: Schedule a Rundeck job |
| 113 |
community.general.rundeck_job_run: |
| 114 |
url: "https://rundeck.example.org" |
| 115 |
api_version: 39 |
| 116 |
api_token: "mytoken" |
| 117 |
job_id: "xxxxxxxxxxxxxxxxx" |
| 118 |
run_at_time: "2021-10-05T15:45:00-03:00" |
| 119 |
register: rundeck_job_schedule |
| 120 |
|
| 121 |
- name: Fire-and-forget a Rundeck job |
| 122 |
community.general.rundeck_job_run: |
| 123 |
url: "https://rundeck.example.org" |
| 124 |
api_version: 39 |
| 125 |
api_token: "mytoken" |
| 126 |
job_id: "xxxxxxxxxxxxxxxxx" |
| 127 |
wait_execution: false |
| 128 |
register: rundeck_job_run |
| 129 |
""" |
| 130 |
|
| 131 |
RETURN = r""" |
| 132 |
execution_info: |
| 133 |
description: Rundeck job execution metadata. |
| 134 |
returned: always |
| 135 |
type: dict |
| 136 |
sample: |
| 137 |
{ |
| 138 |
"msg": "Job execution succeeded!", |
| 139 |
"execution_info": { |
| 140 |
"id": 1, |
| 141 |
"href": "https://rundeck.example.org/api/39/execution/1", |
| 142 |
"permalink": "https://rundeck.example.org/project/myproject/execution/show/1", |
| 143 |
"status": "succeeded", |
| 144 |
"project": "myproject", |
| 145 |
"executionType": "user", |
| 146 |
"user": "admin", |
| 147 |
"date-started": { |
| 148 |
"unixtime": 1633449020784, |
| 149 |
"date": "2021-10-05T15:50:20Z" |
| 150 |
}, |
| 151 |
"date-ended": { |
| 152 |
"unixtime": 1633449026358, |
| 153 |
"date": "2021-10-05T15:50:26Z" |
| 154 |
}, |
| 155 |
"job": { |
| 156 |
"id": "697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a", |
| 157 |
"averageDuration": 4917, |
| 158 |
"name": "Test", |
| 159 |
"group": "", |
| 160 |
"project": "myproject", |
| 161 |
"description": "", |
| 162 |
"options": { |
| 163 |
"exit_code": "0" |
| 164 |
}, |
| 165 |
"href": "https://rundeck.example.org/api/39/job/697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a", |
| 166 |
"permalink": "https://rundeck.example.org/project/myproject/job/show/697af0c4-72d3-4c15-86a3-b5bfe3c6cb6a" |
| 167 |
}, |
| 168 |
"description": "sleep 5 && echo 'Test!' && exit ${option.exit_code}", |
| 169 |
"argstring": "-exit_code 0", |
| 170 |
"serverUUID": "5b9a1438-fa3a-457e-b254-8f3d70338068", |
| 171 |
"successfulNodes": [ |
| 172 |
"localhost" |
| 173 |
], |
| 174 |
"output": "Test!" |
| 175 |
} |
| 176 |
} |
| 177 |
""" |
| 178 |
|
| 179 |
# Modules import |
| 180 |
from datetime import datetime, timedelta |
| 181 |
from time import sleep |
| 182 |
from urllib.parse import quote |
| 183 |
|
| 184 |
from ansible.module_utils.basic import AnsibleModule |
| 185 |
|
| 186 |
from ansible_collections.community.general.plugins.module_utils._rundeck import api_argument_spec, api_request |
| 187 |
|
| 188 |
|
| 189 |
class RundeckJobRun: |
| 190 |
def __init__(self, module): |
| 191 |
self.module = module |
| 192 |
self.url = self.module.params["url"] |
| 193 |
self.api_version = self.module.params["api_version"] |
| 194 |
self.job_id = self.module.params["job_id"] |
| 195 |
self.job_options = self.module.params["job_options"] or {} |
| 196 |
self.filter_nodes = self.module.params["filter_nodes"] or "" |
| 197 |
self.run_at_time = self.module.params["run_at_time"] or "" |
| 198 |
self.loglevel = self.module.params["loglevel"].upper() |
| 199 |
self.wait_execution = self.module.params["wait_execution"] |
| 200 |
self.wait_execution_delay = self.module.params["wait_execution_delay"] |
| 201 |
self.wait_execution_timeout = self.module.params["wait_execution_timeout"] |
| 202 |
self.abort_on_timeout = self.module.params["abort_on_timeout"] |
| 203 |
|
| 204 |
for k, v in self.job_options.items(): |
| 205 |
if not isinstance(v, str): |
| 206 |
self.module.exit_json(msg=f"Job option '{k}' value must be a string", execution_info={}) |
| 207 |
|
| 208 |
def job_status_check(self, execution_id): |
| 209 |
response = dict() |
| 210 |
timeout = False |
| 211 |
due = datetime.now() + timedelta(seconds=self.wait_execution_timeout) |
| 212 |
|
| 213 |
while not timeout: |
| 214 |
endpoint = f"execution/{execution_id}" |
| 215 |
response = api_request(module=self.module, endpoint=endpoint)[0] |
| 216 |
output = api_request(module=self.module, endpoint=f"execution/{execution_id}/output") |
| 217 |
log_output = "\n".join([x["log"] for x in output[0]["entries"]]) |
| 218 |
response.update({"output": log_output}) |
| 219 |
|
| 220 |
if response["status"] == "aborted": |
| 221 |
break |
| 222 |
elif response["status"] == "scheduled": |
| 223 |
self.module.exit_json( |
| 224 |
msg=f"Job scheduled to run at {self.run_at_time}", execution_info=response, changed=True |
| 225 |
) |
| 226 |
elif response["status"] == "failed": |
| 227 |
self.module.fail_json(msg="Job execution failed", execution_info=response) |
| 228 |
elif response["status"] == "succeeded": |
| 229 |
self.module.exit_json(msg="Job execution succeeded!", execution_info=response) |
| 230 |
|
| 231 |
if datetime.now() >= due: |
| 232 |
timeout = True |
| 233 |
break |
| 234 |
|
| 235 |
# Wait for 5s before continue |
| 236 |
sleep(self.wait_execution_delay) |
| 237 |
|
| 238 |
response.update({"timed_out": timeout}) |
| 239 |
return response |
| 240 |
|
| 241 |
def job_run(self): |
| 242 |
response, info = api_request( |
| 243 |
module=self.module, |
| 244 |
endpoint=f"job/{quote(self.job_id)}/run", |
| 245 |
method="POST", |
| 246 |
data={ |
| 247 |
"loglevel": self.loglevel, |
| 248 |
"options": self.job_options, |
| 249 |
"runAtTime": self.run_at_time, |
| 250 |
"filter": self.filter_nodes, |
| 251 |
}, |
| 252 |
) |
| 253 |
|
| 254 |
if info["status"] != 200: |
| 255 |
self.module.fail_json(msg=info["msg"]) |
| 256 |
|
| 257 |
if not self.wait_execution: |
| 258 |
self.module.exit_json(msg="Job run send successfully!", execution_info=response) |
| 259 |
|
| 260 |
job_status = self.job_status_check(response["id"]) |
| 261 |
|
| 262 |
if job_status["timed_out"]: |
| 263 |
if self.abort_on_timeout: |
| 264 |
api_request(module=self.module, endpoint=f"execution/{response['id']}/abort", method="GET") |
| 265 |
|
| 266 |
abort_status = self.job_status_check(response["id"]) |
| 267 |
|
| 268 |
self.module.fail_json( |
| 269 |
msg="Job execution aborted due the timeout specified", execution_info=abort_status |
| 270 |
) |
| 271 |
|
| 272 |
self.module.fail_json(msg="Job execution timed out", execution_info=job_status) |
| 273 |
|
| 274 |
|
| 275 |
def main(): |
| 276 |
argument_spec = api_argument_spec() |
| 277 |
argument_spec.update( |
| 278 |
dict( |
| 279 |
job_id=dict(required=True, type="str"), |
| 280 |
job_options=dict(type="dict"), |
| 281 |
filter_nodes=dict(type="str"), |
| 282 |
run_at_time=dict(type="str"), |
| 283 |
wait_execution=dict(type="bool", default=True), |
| 284 |
wait_execution_delay=dict(type="int", default=5), |
| 285 |
wait_execution_timeout=dict(type="int", default=120), |
| 286 |
abort_on_timeout=dict(type="bool", default=False), |
| 287 |
loglevel=dict(type="str", choices=["debug", "verbose", "info", "warn", "error"], default="info"), |
| 288 |
) |
| 289 |
) |
| 290 |
|
| 291 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) |
| 292 |
|
| 293 |
if module.params["api_version"] < 14: |
| 294 |
module.fail_json(msg="API version should be at least 14") |
| 295 |
|
| 296 |
rundeck = RundeckJobRun(module) |
| 297 |
rundeck.job_run() |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__": |
| 301 |
main() |