4747import ansible .executor .process .worker
4848import ansible .template
4949import ansible .utils .sentinel
50+ import ansible .playbook .play_context
51+ import ansible .plugins .loader
5052
5153
5254def _patch_awx_callback ():
@@ -76,12 +78,12 @@ def patch_add_local(self, **kwargs):
7678_patch_awx_callback ()
7779
7880
79- def wrap_action_loader__get (name , * args , ** kwargs ):
81+ def wrap_action_loader__get_with_context (name , * args , ** kwargs ):
8082 """
81- While the mitogen strategy is active, trap action_loader.get() calls,
82- augmenting any fetched class with ActionModuleMixin, which replaces various
83- helper methods inherited from ActionBase with implementations that avoid
84- the use of shell fragments wherever possible.
83+ While the mitogen strategy is active, trap action_loader.get_with_context()
84+ calls, augmenting any fetched class with ActionModuleMixin, which replaces
85+ various helper methods inherited from ActionBase with implementations that
86+ avoid the use of shell fragments wherever possible.
8587
8688 This is used instead of static subclassing as it generalizes to third party
8789 action plugins outside the Ansible tree.
@@ -91,13 +93,26 @@ def wrap_action_loader__get(name, *args, **kwargs):
9193 name = 'mitogen_' + name
9294 get_kwargs ['collection_list' ] = kwargs .pop ('collection_list' , None )
9395
94- klass = ansible_mitogen .loaders .action_loader__get (name , ** get_kwargs )
96+ (klass , context ) = ansible_mitogen .loaders .action_loader__get_with_context (
97+ name ,
98+ ** get_kwargs
99+ )
100+
95101 if klass :
96102 bases = (ansible_mitogen .mixins .ActionModuleMixin , klass )
97103 adorned_klass = type (str (name ), bases , {})
98104 if kwargs .get ('class_only' ):
99- return adorned_klass
100- return adorned_klass (* args , ** kwargs )
105+ return ansible .plugins .loader .get_with_context_result (
106+ adorned_klass ,
107+ context
108+ )
109+
110+ return ansible .plugins .loader .get_with_context_result (
111+ adorned_klass (* args , ** kwargs ),
112+ context
113+ )
114+
115+ return ansible .plugins .loader .get_with_context_result (None , context )
101116
102117
103118REDIRECTED_CONNECTION_PLUGINS = (
@@ -115,15 +130,26 @@ def wrap_action_loader__get(name, *args, **kwargs):
115130)
116131
117132
118- def wrap_connection_loader__get (name , * args , ** kwargs ):
133+ def wrap_connection_loader__get_with_context (name , * args , ** kwargs ):
119134 """
120- While a Mitogen strategy is active, rewrite connection_loader.get() calls
121- for some transports into requests for a compatible Mitogen transport.
135+ While a Mitogen strategy is active, rewrite
136+ connection_loader.get_with_context() calls for some transports into
137+ requests for a compatible Mitogen transport.
122138 """
123- if name in REDIRECTED_CONNECTION_PLUGINS :
139+ is_play_using_mitogen_connection = None
140+ if len (args ) > 0 and isinstance (args [0 ], ansible .playbook .play_context .PlayContext ):
141+ play_context = args [0 ]
142+ is_play_using_mitogen_connection = play_context .connection in REDIRECTED_CONNECTION_PLUGINS
143+
144+ # assume true if we're not in a play context since we're using a Mitogen strategy
145+ if is_play_using_mitogen_connection is None :
146+ is_play_using_mitogen_connection = True
147+
148+ redirect_connection = name in REDIRECTED_CONNECTION_PLUGINS and is_play_using_mitogen_connection
149+ if redirect_connection :
124150 name = 'mitogen_' + name
125151
126- return ansible_mitogen .loaders .connection_loader__get (name , * args , ** kwargs )
152+ return ansible_mitogen .loaders .connection_loader__get_with_context (name , * args , ** kwargs )
127153
128154
129155def wrap_worker__run (self ):
@@ -173,8 +199,8 @@ def _install_wrappers(self):
173199 Install our PluginLoader monkey patches and update global variables
174200 with references to the real functions.
175201 """
176- ansible_mitogen .loaders .action_loader .get = wrap_action_loader__get
177- ansible_mitogen .loaders .connection_loader .get_with_context = wrap_connection_loader__get
202+ ansible_mitogen .loaders .action_loader .get_with_context = wrap_action_loader__get_with_context
203+ ansible_mitogen .loaders .connection_loader .get_with_context = wrap_connection_loader__get_with_context
178204
179205 global worker__run
180206 worker__run = ansible .executor .process .worker .WorkerProcess .run
@@ -184,11 +210,11 @@ def _remove_wrappers(self):
184210 """
185211 Uninstall the PluginLoader monkey patches.
186212 """
187- ansible_mitogen .loaders .action_loader .get = (
188- ansible_mitogen .loaders .action_loader__get
213+ ansible_mitogen .loaders .action_loader .get_with_context = (
214+ ansible_mitogen .loaders .action_loader__get_with_context
189215 )
190216 ansible_mitogen .loaders .connection_loader .get_with_context = (
191- ansible_mitogen .loaders .connection_loader__get
217+ ansible_mitogen .loaders .connection_loader__get_with_context
192218 )
193219 ansible .executor .process .worker .WorkerProcess .run = worker__run
194220
0 commit comments