Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_tasks_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2021, Andreas Botzner (@paginabianca) <andreas at botzner dot com>
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
8 DOCUMENTATION = r"""
9 module: proxmox_tasks_info
10 short_description: Retrieve information about one or more Proxmox VE tasks
11 description:
12 - Retrieve information about one or more Proxmox VE tasks.
13 author: 'Andreas Botzner (@paginabianca) <andreas at botzner dot com>'
14 options:
15 node:
16 description:
17 - Node where to get tasks.
18 required: true
19 type: str
20 task:
21 description:
22 - Return specific task.
23 aliases: ['upid', 'name']
24 type: str
25 source:
26 description:
27 - Source of tasks to be considered.
28 type: str
29 choices: ['archive', 'active', 'all']
30 default: archive
31 extends_documentation_fragment:
32 - community.proxmox.proxmox.actiongroup_proxmox
33 - community.proxmox.proxmox.documentation
34 - community.proxmox.attributes
35 - community.proxmox.attributes.info_module
36 """
37
38
39 EXAMPLES = r"""
40 - name: List finished tasks on node01
41 community.proxmox.proxmox_tasks_info:
42 node: node01
43 register: result
44
45 - name: List active tasks on node02
46 community.proxmox.proxmox_tasks_info:
47 node: node02
48 source: active
49 register: result
50
51 - name: Retrieve information about specific tasks on node01
52 community.proxmox.proxmox_tasks_info:
53 task: 'UPID:node01:00003263:16167ACE:621EE230:srvreload:networking:root@pam:'
54 node: node01
55 register: proxmox_tasks
56 """
57
58
59 RETURN = r"""
60 proxmox_tasks:
61 description: List of tasks.
62 returned: on success
63 type: list
64 elements: dict
65 contains:
66 id:
67 description: ID of the task.
68 returned: on success
69 type: str
70 node:
71 description: Node name.
72 returned: on success
73 type: str
74 pid:
75 description: PID of the task.
76 returned: on success
77 type: int
78 pstart:
79 description: Pastart of the task.
80 returned: on success
81 type: int
82 starttime:
83 description: Starting time of the task.
84 returned: on success
85 type: int
86 type:
87 description: Type of the task.
88 returned: on success
89 type: str
90 upid:
91 description: UPID of the task.
92 returned: on success
93 type: str
94 user:
95 description: User that owns the task.
96 returned: on success
97 type: str
98 endtime:
99 description: Endtime of the task.
100 returned: on success, can be absent
101 type: int
102 status:
103 description: Status of the task.
104 returned: on success, can be absent
105 type: str
106 failed:
107 description: If the task failed.
108 returned: when status is defined
109 type: bool
110 msg:
111 description: Short message.
112 returned: on failure
113 type: str
114 sample: 'Task: UPID:xyz:xyz does not exist on node: proxmoxnode'
115 """
116
117 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
118 ProxmoxAnsible,
119 create_proxmox_module,
120 )
121
122
123 def module_args():
124 return dict(
125 task=dict(type="str", aliases=["upid", "name"], required=False),
126 node=dict(type="str", required=True),
127 source=dict(default="archive", choices=["archive", "active", "all"]),
128 )
129
130
131 def module_options():
132 return {}
133
134
135 class ProxmoxTaskInfoAnsible(ProxmoxAnsible):
136 def get_task(self, upid, node, source):
137 tasks = self.get_tasks(node, source)
138 for task in tasks:
139 if task.info["upid"] == upid:
140 return [task]
141
142 def get_tasks(self, node, source):
143 tasks = self.proxmox_api.nodes(node).tasks.get(source=source)
144 return [ProxmoxTask(task) for task in tasks]
145
146
147 class ProxmoxTask:
148 def __init__(self, task):
149 self.info = dict()
150 for k, v in task.items():
151 if k == "status" and isinstance(v, str):
152 self.info[k] = v
153 if v != "OK":
154 self.info["failed"] = True
155 else:
156 self.info[k] = v
157
158
159 def main():
160 module = create_proxmox_module(module_args(), **module_options())
161 proxmox = ProxmoxTaskInfoAnsible(module)
162
163 result = dict(changed=False)
164
165 upid = module.params["task"]
166 node = module.params["node"]
167 source = module.params["source"]
168 if upid:
169 tasks = proxmox.get_task(upid=upid, node=node, source=source)
170 else:
171 tasks = proxmox.get_tasks(node=node, source=source)
172 if tasks is not None:
173 result["proxmox_tasks"] = [task.info for task in tasks]
174 module.exit_json(**result)
175 else:
176 result["msg"] = f"Task: {upid} does not exist on node: {node}."
177 module.fail_json(**result)
178
179
180 if __name__ == "__main__":
181 main()