99from ansible .executor .playbook_executor import PlaybookExecutor
1010from ansible .playbook .play import Play
1111import ansible .constants as C
12+ from ansible .utils .display import Display
1213
1314from .callback import AdHocResultCallback , PlaybookResultCallBack , \
1415 CommandResultCallback
2122logger = get_logger (__name__ )
2223
2324
25+ class CustomDisplay (Display ):
26+ def display (self , msg , color = None , stderr = False , screen_only = False , log_only = False ):
27+ pass
28+
29+ display = CustomDisplay ()
30+
31+
2432Options = namedtuple ('Options' , [
2533 'listtags' , 'listtasks' , 'listhosts' , 'syntax' , 'connection' ,
2634 'module_path' , 'forks' , 'remote_user' , 'private_key_file' , 'timeout' ,
@@ -123,20 +131,22 @@ class AdHocRunner:
123131 ADHoc Runner接口
124132 """
125133 results_callback_class = AdHocResultCallback
134+ results_callback = None
126135 loader_class = DataLoader
127136 variable_manager_class = VariableManager
128- options = get_default_options ()
129137 default_options = get_default_options ()
130138
131139 def __init__ (self , inventory , options = None ):
132- if options :
133- self .options = options
140+ self .options = self .update_options (options )
134141 self .inventory = inventory
135142 self .loader = DataLoader ()
136143 self .variable_manager = VariableManager (
137144 loader = self .loader , inventory = self .inventory
138145 )
139146
147+ def get_result_callback (self , file_obj = None ):
148+ return self .__class__ .results_callback_class (file_obj = file_obj )
149+
140150 @staticmethod
141151 def check_module_args (module_name , module_args = '' ):
142152 if module_name in C .MODULE_REQUIRE_ARGS and not module_args :
@@ -160,19 +170,24 @@ def clean_tasks(self, tasks):
160170 cleaned_tasks .append (task )
161171 return cleaned_tasks
162172
163- def set_option (self , k , v ):
164- kwargs = {k : v }
165- self .options = self .options ._replace (** kwargs )
173+ def update_options (self , options ):
174+ if options and isinstance (options , dict ):
175+ options = self .__class__ .default_options ._replace (** options )
176+ else :
177+ options = self .__class__ .default_options
178+ return options
166179
167- def run (self , tasks , pattern , play_name = 'Ansible Ad-hoc' , gather_facts = 'no' ):
180+ def run (self , tasks , pattern , play_name = 'Ansible Ad-hoc' , gather_facts = 'no' , file_obj = None ):
168181 """
169182 :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ]
170183 :param pattern: all, *, or others
171184 :param play_name: The play name
185+ :param gather_facts:
186+ :param file_obj: logging to file_obj
172187 :return:
173188 """
174189 self .check_pattern (pattern )
175- results_callback = self .results_callback_class ( )
190+ self . results_callback = self .get_result_callback ( file_obj )
176191 cleaned_tasks = self .clean_tasks (tasks )
177192
178193 play_source = dict (
@@ -193,16 +208,16 @@ def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no'):
193208 variable_manager = self .variable_manager ,
194209 loader = self .loader ,
195210 options = self .options ,
196- stdout_callback = results_callback ,
211+ stdout_callback = self . results_callback ,
197212 passwords = self .options .passwords ,
198213 )
199- logger . debug ("Get inventory matched hosts: {}" .format (
214+ print ("Get matched hosts: {}" .format (
200215 self .inventory .get_matched_hosts (pattern )
201216 ))
202217
203218 try :
204219 tqm .run (play )
205- return results_callback
220+ return self . results_callback
206221 except Exception as e :
207222 raise AnsibleError (e )
208223 finally :
0 commit comments