Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/wait_for_task.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2023, Julien Godin <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21 DOCUMENTATION = '''
22 ---
23 module: wait_for_task
24 version_added: 3.13.0
25 short_description: Wait for a task
26 description:
27 - Wait for a task to finish
28 author:
29 - "Julien Godin (@JGodin-C2C)"
30 options:
31 task:
32 description:
33 - Task id to wait for.
34 required: true
35 type: str
36 timeout:
37 description:
38 - How much time the task should take to be finished
39 required: false
40 type: int
41 default: 60
42 extends_documentation_fragment:
43 - theforeman.foreman.foreman
44 '''
45
46
47 EXAMPLES = '''
48 - name: Wait for a task to finish
49 theforeman.foreman.wait_for_task:
50 server_url: "https://foreman.example.com"
51 password: changeme
52 username: admin
53 task: a03ba49f-4dc2-4ad6-a48b-b271b46f3347
54 timeout: 60
55
56 - name: Sarch for previously created tasks
57 resource_info:
58 server_url: "https://foreman.example.com"
59 password: changeme
60 username: admin
61 resource: foreman_tasks
62 search: "(label = Actions::Katello::Product::Destroy and action ~ 'Test Product' and state = running)"
63 register: tasks
64
65 - name: Wait for all found tasks to finish
66 wait_for_task:
67 server_url: "https://foreman.example.com"
68 password: changeme
69 username: admin
70 task: "{{ item }}"
71 timeout: 900
72 loop: "{{ tasks.resources | map(attribute='id') | list }}"
73 '''
74
75 RETURN = '''
76 task:
77 description: The finished task
78 returned: success
79 type: dict
80 '''
81
82
83 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanAnsibleModule
84
85
86 class ForemanWaitForTask(ForemanAnsibleModule):
87 pass
88
89
90 def main():
91 module = ForemanWaitForTask(
92 argument_spec=dict(
93 task=dict(type="str", required=True),
94 timeout=dict(type="int", required=False, default=60),
95 )
96 )
97 module.task_timeout = module.foreman_params["timeout"]
98 with module.api_connection():
99
100 task = module.wait_for_task(module.show_resource(
101 'foreman_tasks', module.foreman_params["task"]))
102 module.exit_json(task=task, task_id=task['id'])
103
104
105 if __name__ == "__main__":
106 main()