Debian Codegraph @

lib/ansible/modules/async_status.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright: (c) 2012, Michael DeHaan <[email protected]>, and others
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 import sys
9
10 DOCUMENTATION = r"""
11 ---
12 module: async_status
13 short_description: Obtain status of asynchronous task
14 description:
15 - This module gets the status of an asynchronous task.
16 - This module is also supported for Windows targets.
17 version_added: "0.5"
18 options:
19 jid:
20 description:
21 - Job or task identifier
22 type: str
23 required: true
24 mode:
25 description:
26 - If V(status), obtain the status.
27 - If V(cleanup), clean up the async job cache (by default in C(~/.ansible_async/)) for the specified job O(jid), without waiting for it to finish.
28 type: str
29 choices: [ cleanup, status ]
30 default: status
31 notes:
32 - The RV(started) and RV(finished) return values were updated to return V(True) or V(False) instead of V(1) or V(0) in ansible-core 2.19.
33 extends_documentation_fragment:
34 - action_common_attributes
35 - action_common_attributes.flow
36 attributes:
37 action:
38 support: full
39 async:
40 support: none
41 check_mode:
42 support: full
43 version_added: '2.17'
44 diff_mode:
45 support: none
46 bypass_host_loop:
47 support: none
48 platform:
49 support: full
50 platforms: posix, windows
51 seealso:
52 - ref: playbooks_async
53 description: Detailed information on how to use asynchronous actions and polling.
54 author:
55 - Ansible Core Team
56 - Michael DeHaan
57 """
58
59 EXAMPLES = r"""
60 ---
61 - name: Asynchronous dnf task
62 ansible.builtin.dnf:
63 name: docker-io
64 state: present
65 async: 1000
66 poll: 0
67 register: dnf_sleeper
68
69 - name: Wait for asynchronous job to end
70 ansible.builtin.async_status:
71 jid: '{{ dnf_sleeper.ansible_job_id }}'
72 register: job_result
73 until: job_result is finished
74 retries: 100
75 delay: 10
76
77 - name: Clean up async file
78 ansible.builtin.async_status:
79 jid: '{{ dnf_sleeper.ansible_job_id }}'
80 mode: cleanup
81 """
82
83 RETURN = r"""
84 ansible_job_id:
85 description: The asynchronous job id
86 returned: success
87 type: str
88 sample: '360874038559.4169'
89 finished:
90 description: Whether the asynchronous job has finished or not
91 returned: always
92 type: bool
93 sample: true
94 started:
95 description: Whether the asynchronous job has started or not
96 returned: always
97 type: bool
98 sample: true
99 stdout:
100 description: Any output returned by async_wrapper
101 returned: always
102 type: str
103 stderr:
104 description: Any errors returned by async_wrapper
105 returned: always
106 type: str
107 erased:
108 description: Path to erased job file
109 returned: when file is erased
110 type: str
111 """
112
113 import json
114 import os
115
116 from ansible.module_utils.basic import AnsibleModule
117
118
119 def main():
120
121 module = AnsibleModule(
122 argument_spec=dict(
123 jid=dict(type="str", required=True),
124 mode=dict(type="str", default="status", choices=["cleanup", "status"]),
125 # passed in from the async_status action plugin
126 _async_dir=dict(type="path", required=True),
127 ),
128 supports_check_mode=True,
129 )
130
131 mode = module.params['mode']
132 jid = module.params['jid']
133 async_dir = module.params['_async_dir']
134
135 # setup logging directory
136 log_path = os.path.join(async_dir, jid)
137
138 if not os.path.exists(log_path):
139 module.fail_json(msg="could not find job", ansible_job_id=jid, started=True, finished=True)
140
141 if mode == 'cleanup':
142 os.unlink(log_path)
143 module.exit_json(ansible_job_id=jid, erased=log_path)
144
145 # NOT in cleanup mode, assume regular status mode
146 # no remote kill mode currently exists, but probably should
147 # consider log_path + ".pid" file and also unlink that above
148
149 data = None
150 try:
151 with open(log_path) as f:
152 data = json.loads(f.read())
153 except Exception:
154 if not data:
155 # file not written yet? That means it is running
156 module.exit_json(results_file=log_path, ansible_job_id=jid, started=True, finished=False)
157 else:
158 module.fail_json(ansible_job_id=jid, results_file=log_path,
159 msg="Could not parse job output: %s" % data, started=True, finished=True)
160
161 if 'started' not in data:
162 data['finished'] = True
163 data['ansible_job_id'] = jid
164 elif 'finished' not in data:
165 data['finished'] = False
166
167 # just write the module output directly to stdout and exit; bypass other processing done by exit_json since it's already been done
168 print(f"\n{json.dumps(data)}") # pylint: disable=ansible-bad-function
169 sys.exit(0) # pylint: disable=ansible-bad-function
170
171
172 if __name__ == '__main__':
173 main()