Debian Codegraph @

ansible_collections/community/windows/plugins/action/win_pssession_configuration.py

raw
1 # Copyright: (c) 2020, Brian Scholer <@briantist>
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import (absolute_import, division, print_function)
5 __metaclass__ = type
6
7 import time
8
9 from ansible.errors import AnsibleError
10 from ansible.plugins.action import ActionBase
11 from ansible.playbook.task import Task
12 from ansible.utils.display import Display
13 display = Display()
14
15
16 def clean_async_result(reference_keys, obj):
17 for key in reference_keys:
18 obj.pop(key)
19 return obj
20
21
22 class ActionModule(ActionBase):
23 _default_async_timeout = 300
24 _default_async_poll = 1
25
26 def __init__(self, *args, **kwargs):
27 super(ActionModule, self).__init__(*args, **kwargs)
28
29 def run(self, tmp=None, task_vars=None):
30 self._supports_check_mode = True
31 self._supports_async = False
32 check_mode = self._task.check_mode
33 async_timeout = self._task.args.get('async_timeout', self._default_async_timeout)
34 async_poll = self._task.args.get('async_poll', self._default_async_poll)
35
36 result = super(ActionModule, self).run(tmp, task_vars)
37
38 if async_poll <= 0:
39 raise AnsibleError("The 'async_poll' option must be greater than 0, got: %i" % async_poll)
40
41 # build the wait_for_connection object for later use
42 wait_for_connection_task = self._task.copy()
43 wait_for_connection_task.args = {
44 'timeout': async_timeout,
45 'sleep': async_poll,
46 }
47 wait_connection_action = self._shared_loader_obj.action_loader.get(
48 'wait_for_connection',
49 task=wait_for_connection_task,
50 connection=self._connection,
51 play_context=self._play_context,
52 loader=self._loader,
53 templar=self._templar,
54 shared_loader_obj=self._shared_loader_obj
55 )
56
57 # if it's not in check mode, call the module async so the WinRM restart doesn't kill ansible
58 if not check_mode:
59 self._task.async_val = async_timeout
60 self._task.poll = async_poll
61
62 result = status = self._execute_module(
63 task_vars=task_vars,
64 module_args=self._task.args
65 )
66 display.vvvv("Internal Async Result: %r" % status)
67
68 # if we're in check mode (not doing async) return the result now
69 if check_mode:
70 return result
71
72 # turn off async so we don't run the following actions as async
73 self._task.async_val = 0
74
75 # build the async_status object
76 async_status_load_params = dict(
77 action='async_status jid=%s' % status['ansible_job_id'],
78 environment=self._task.environment
79 )
80 async_status_task = Task().load(async_status_load_params)
81 async_status_action = self._shared_loader_obj.action_loader.get(
82 'async_status',
83 task=async_status_task,
84 connection=self._connection,
85 play_context=self._play_context,
86 loader=self._loader,
87 templar=self._templar,
88 shared_loader_obj=self._shared_loader_obj
89 )
90
91 # build an async_status mode=cleanup object
92 async_cleanup_load_params = dict(
93 action='async_status mode=cleanup jid=%s' % status['ansible_job_id'],
94 environment=self._task.environment
95 )
96 async_cleanup_task = Task().load(async_cleanup_load_params)
97 async_cleanup_action = self._shared_loader_obj.action_loader.get(
98 'async_status',
99 task=async_cleanup_task,
100 connection=self._connection,
101 play_context=self._play_context,
102 loader=self._loader,
103 templar=self._templar,
104 shared_loader_obj=self._shared_loader_obj
105 )
106
107 # Retries here is a fallback in case the module fails in an unexpected way
108 # which can sometimes not properly set the failed field in the return.
109 # It is not related to async retries.
110 # Without this, that situation would cause an infinite loop.
111 max_retries = 3
112 retries = 0
113 while not check_mode:
114 try:
115 # check up on the async job
116 job_status = async_status_action.run(task_vars=task_vars)
117 display.vvvv("Async Job Status: %r" % job_status)
118
119 if job_status.get('failed', False):
120 raise AnsibleError(job_status.get('msg', job_status))
121
122 if job_status.get('finished', False):
123 result = job_status
124 break
125
126 time.sleep(self._task.poll)
127
128 except BaseException as e:
129 retries += 1
130 if retries >= max_retries:
131 display.vvvv("Max retries reached.")
132 raise e
133 display.vvvv("Retrying (%s of %s)" % (retries, max_retries))
134 display.vvvv("Falling back to wait_for_connection: %r" % e)
135 wait_connection_action.run(task_vars=task_vars)
136
137 try:
138 # let's try to clean up after our implicit async
139 job_status = async_cleanup_action.run(task_vars=task_vars)
140 if job_status.get('failed', False):
141 display.vvvv("Clean up of async status failed on the remote host: %r" % job_status.get('msg', job_status))
142 except BaseException as e:
143 # let's swallow errors during implicit cleanup to aovid interrupting what was otherwise a successful run
144 display.vvvv("Clean up of async status failed on the remote host: %r" % e)
145
146 return clean_async_result(status.keys(), result)