Debian Codegraph @

ansible_collections/community/general/plugins/modules/nomad_job_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2020, FERREIRA Christophe <[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: nomad_job_info
11 author: FERREIRA Christophe (@chris93111)
12 version_added: "1.3.0"
13 short_description: Get Nomad Jobs info
14 description:
15 - Get info for one Nomad job.
16 - List Nomad jobs.
17 requirements:
18 - python-nomad
19 extends_documentation_fragment:
20 - community.general._nomad
21 - community.general._attributes
22 - community.general._attributes.info_module
23 options:
24 name:
25 description:
26 - Name of job for Get info.
27 - If not specified, lists all jobs.
28 type: str
29 seealso:
30 - name: Nomad jobs documentation
31 description: Complete documentation for Nomad API jobs.
32 link: https://www.nomadproject.io/api-docs/jobs/
33 """
34
35 EXAMPLES = r"""
36 - name: Get info for job awx
37 community.general.nomad_job_info:
38 host: localhost
39 name: awx
40 register: result
41
42 - name: List Nomad jobs
43 community.general.nomad_job_info:
44 host: localhost
45 register: result
46 """
47
48 RETURN = r"""
49 result:
50 description: List with dictionary contains jobs info.
51 returned: success
52 type: list
53 sample:
54 [
55 {
56 "Affinities": null,
57 "AllAtOnce": false,
58 "Constraints": null,
59 "ConsulToken": "",
60 "CreateIndex": 13,
61 "Datacenters": [
62 "dc1"
63 ],
64 "Dispatched": false,
65 "ID": "example",
66 "JobModifyIndex": 13,
67 "Meta": null,
68 "ModifyIndex": 13,
69 "Multiregion": null,
70 "Name": "example",
71 "Namespace": "default",
72 "NomadTokenID": "",
73 "ParameterizedJob": null,
74 "ParentID": "",
75 "Payload": null,
76 "Periodic": null,
77 "Priority": 50,
78 "Region": "global",
79 "Spreads": null,
80 "Stable": false,
81 "Status": "pending",
82 "StatusDescription": "",
83 "Stop": false,
84 "SubmitTime": 1602244370615307000,
85 "TaskGroups": [
86 {
87 "Affinities": null,
88 "Constraints": null,
89 "Count": 1,
90 "EphemeralDisk": {
91 "Migrate": false,
92 "SizeMB": 300,
93 "Sticky": false
94 },
95 "Meta": null,
96 "Migrate": {
97 "HealthCheck": "checks",
98 "HealthyDeadline": 300000000000,
99 "MaxParallel": 1,
100 "MinHealthyTime": 10000000000
101 },
102 "Name": "cache",
103 "Networks": null,
104 "ReschedulePolicy": {
105 "Attempts": 0,
106 "Delay": 30000000000,
107 "DelayFunction": "exponential",
108 "Interval": 0,
109 "MaxDelay": 3600000000000,
110 "Unlimited": true
111 },
112 "RestartPolicy": {
113 "Attempts": 3,
114 "Delay": 15000000000,
115 "Interval": 1800000000000,
116 "Mode": "fail"
117 },
118 "Scaling": null,
119 "Services": null,
120 "ShutdownDelay": null,
121 "Spreads": null,
122 "StopAfterClientDisconnect": null,
123 "Tasks": [
124 {
125 "Affinities": null,
126 "Artifacts": null,
127 "CSIPluginConfig": null,
128 "Config": {
129 "image": "redis:3.2",
130 "port_map": [
131 {
132 "db": 6379.0
133 }
134 ]
135 },
136 "Constraints": null,
137 "DispatchPayload": null,
138 "Driver": "docker",
139 "Env": null,
140 "KillSignal": "",
141 "KillTimeout": 5000000000,
142 "Kind": "",
143 "Leader": false,
144 "Lifecycle": null,
145 "LogConfig": {
146 "MaxFileSizeMB": 10,
147 "MaxFiles": 10
148 },
149 "Meta": null,
150 "Name": "redis",
151 "Resources": {
152 "CPU": 500,
153 "Devices": null,
154 "DiskMB": 0,
155 "IOPS": 0,
156 "MemoryMB": 256,
157 "Networks": [
158 {
159 "CIDR": "",
160 "DNS": null,
161 "Device": "",
162 "DynamicPorts": [
163 {
164 "HostNetwork": "default",
165 "Label": "db",
166 "To": 0,
167 "Value": 0
168 }
169 ],
170 "IP": "",
171 "MBits": 10,
172 "Mode": "",
173 "ReservedPorts": null
174 }
175 ]
176 },
177 "RestartPolicy": {
178 "Attempts": 3,
179 "Delay": 15000000000,
180 "Interval": 1800000000000,
181 "Mode": "fail"
182 },
183 "Services": [
184 {
185 "AddressMode": "auto",
186 "CanaryMeta": null,
187 "CanaryTags": null,
188 "Checks": [
189 {
190 "AddressMode": "",
191 "Args": null,
192 "CheckRestart": null,
193 "Command": "",
194 "Expose": false,
195 "FailuresBeforeCritical": 0,
196 "GRPCService": "",
197 "GRPCUseTLS": false,
198 "Header": null,
199 "InitialStatus": "",
200 "Interval": 10000000000,
201 "Method": "",
202 "Name": "alive",
203 "Path": "",
204 "PortLabel": "",
205 "Protocol": "",
206 "SuccessBeforePassing": 0,
207 "TLSSkipVerify": false,
208 "TaskName": "",
209 "Timeout": 2000000000,
210 "Type": "tcp"
211 }
212 ],
213 "Connect": null,
214 "EnableTagOverride": false,
215 "Meta": null,
216 "Name": "redis-cache",
217 "PortLabel": "db",
218 "Tags": [
219 "global",
220 "cache"
221 ],
222 "TaskName": ""
223 }
224 ],
225 "ShutdownDelay": 0,
226 "Templates": null,
227 "User": "",
228 "Vault": null,
229 "VolumeMounts": null
230 }
231 ],
232 "Update": {
233 "AutoPromote": false,
234 "AutoRevert": false,
235 "Canary": 0,
236 "HealthCheck": "checks",
237 "HealthyDeadline": 180000000000,
238 "MaxParallel": 1,
239 "MinHealthyTime": 10000000000,
240 "ProgressDeadline": 600000000000,
241 "Stagger": 30000000000
242 },
243 "Volumes": null
244 }
245 ],
246 "Type": "service",
247 "Update": {
248 "AutoPromote": false,
249 "AutoRevert": false,
250 "Canary": 0,
251 "HealthCheck": "",
252 "HealthyDeadline": 0,
253 "MaxParallel": 1,
254 "MinHealthyTime": 0,
255 "ProgressDeadline": 0,
256 "Stagger": 30000000000
257 },
258 "VaultNamespace": "",
259 "VaultToken": "",
260 "Version": 0
261 }
262 ]
263 """
264
265 from ansible.module_utils.basic import AnsibleModule
266
267 from ansible_collections.community.general.plugins.module_utils._nomad import argument_spec as nomad_argument_spec
268 from ansible_collections.community.general.plugins.module_utils._nomad import setup_nomad_client
269
270
271 def run():
272 module = AnsibleModule(
273 argument_spec={
274 **nomad_argument_spec,
275 "name": dict(type="str"),
276 },
277 supports_check_mode=True,
278 )
279
280 nomad_client = setup_nomad_client(module)
281
282 changed = False
283 result = list()
284 try:
285 job_list = nomad_client.jobs.get_jobs()
286 for job in job_list:
287 result.append(nomad_client.job.get_job(job.get("ID")))
288 except Exception as e:
289 module.fail_json(msg=f"{e}")
290
291 if module.params["name"]:
292 filter = list()
293 try:
294 for job in result:
295 if job.get("ID") == module.params["name"]:
296 filter.append(job)
297 result = filter
298 if not filter:
299 module.fail_json(msg=f"Couldn't find Job with id {module.params['name']}")
300 except Exception as e:
301 module.fail_json(msg=f"{e}")
302
303 module.exit_json(changed=changed, result=result)
304
305
306 def main():
307 run()
308
309
310 if __name__ == "__main__":
311 main()