25
25
else :
26
26
_has_ansible = True
27
27
_ansible_major_version = int (ansible .__version__ .split ("." , 1 )[0 ])
28
+ import ansible .constants
28
29
if _ansible_major_version == 1 :
29
30
import ansible .inventory
30
31
import ansible .runner
32
+ import ansible .utils
31
33
elif _ansible_major_version == 2 :
34
+ import ansible .cli
32
35
import ansible .executor .task_queue_manager
33
36
import ansible .inventory
34
37
import ansible .parsing .dataloader
35
38
import ansible .playbook .play
36
39
import ansible .plugins .callback
40
+ import ansible .utils .vars
37
41
import ansible .vars
38
42
39
43
44
+ def _reload_constants ():
45
+ # Reload defaults that can depend on environment variables and
46
+ # current working directory
47
+ reload (ansible .constants )
48
+
49
+
40
50
class AnsibleRunnerBase (object ):
41
51
42
52
def __init__ (self , host_list = None ):
@@ -71,7 +81,13 @@ class AnsibleRunnerV1(AnsibleRunnerBase):
71
81
72
82
def __init__ (self , host_list = None ):
73
83
super (AnsibleRunnerV1 , self ).__init__ (host_list )
74
- self .inventory = ansible .inventory .Inventory (self .host_list )
84
+ _reload_constants ()
85
+ self .vault_pass = ansible .utils .read_vault_file (
86
+ ansible .constants .DEFAULT_VAULT_PASSWORD_FILE )
87
+ kwargs = {"vault_password" : self .vault_pass }
88
+ if self .host_list is not None :
89
+ kwargs ["host_list" ] = host_list
90
+ self .inventory = ansible .inventory .Inventory (** kwargs )
75
91
76
92
def get_hosts (self , pattern = None ):
77
93
return [
@@ -91,6 +107,8 @@ def run(self, host, module_name, module_args=None, **kwargs):
91
107
result = ansible .runner .Runner (
92
108
pattern = host ,
93
109
module_name = module_name ,
110
+ vault_pass = self .vault_pass ,
111
+ inventory = self .inventory ,
94
112
** kwargs ).run ()
95
113
if host not in result ["contacted" ]:
96
114
raise RuntimeError ("Unexpected error: {}" .format (result ))
@@ -100,21 +118,6 @@ def run(self, host, module_name, module_args=None, **kwargs):
100
118
return result ["contacted" ][host ]
101
119
102
120
103
- class Options (object ):
104
-
105
- def __init__ (self , ** kwargs ):
106
- self .connection = "smart"
107
- for attr in (
108
- "module_path" , "forks" , "remote_user" , "private_key_file" ,
109
- "ssh_common_args" , "ssh_extra_args" , "sftp_extra_args" ,
110
- "scp_extra_args" , "become" , "become_method" , "become_user" ,
111
- "verbosity" ,
112
- ):
113
- setattr (self , attr , None )
114
- self .check = kwargs .get ("check" , False )
115
- super (Options , self ).__init__ ()
116
-
117
-
118
121
if _has_ansible and _ansible_major_version == 2 :
119
122
class Callback (ansible .plugins .callback .CallbackBase ):
120
123
@@ -147,12 +150,31 @@ class AnsibleRunnerV2(AnsibleRunnerBase):
147
150
148
151
def __init__ (self , host_list = None ):
149
152
super (AnsibleRunnerV2 , self ).__init__ (host_list )
153
+ _reload_constants ()
150
154
self .variable_manager = ansible .vars .VariableManager ()
155
+ self .options = ansible .cli .CLI (None ).base_parser (
156
+ connect_opts = True ,
157
+ meta_opts = True ,
158
+ runas_opts = True ,
159
+ subset_opts = True ,
160
+ check_opts = True ,
161
+ inventory_opts = True ,
162
+ runtask_opts = True ,
163
+ vault_opts = True ,
164
+ fork_opts = True ,
165
+ module_opts = True ,
166
+ ).parse_args ([])[0 ]
167
+ self .options .connection = "smart"
151
168
self .loader = ansible .parsing .dataloader .DataLoader ()
169
+ if self .options .vault_password_file :
170
+ vault_pass = ansible .cli .CLI .read_vault_password_file (
171
+ self .options .vault_password_file , loader = self .loader )
172
+ self .loader .set_vault_password (vault_pass )
173
+
152
174
self .inventory = ansible .inventory .Inventory (
153
175
loader = self .loader ,
154
176
variable_manager = self .variable_manager ,
155
- host_list = host_list ,
177
+ host_list = host_list or self . options . inventory ,
156
178
)
157
179
self .variable_manager .set_inventory (self .inventory )
158
180
@@ -163,9 +185,12 @@ def get_hosts(self, pattern=None):
163
185
]
164
186
165
187
def get_variables (self , host ):
166
- return self .inventory .get_vars (host )
188
+ host = self .inventory .get_host (host )
189
+ return ansible .utils .vars .combine_vars (
190
+ host .get_group_vars (), host .get_vars ())
167
191
168
192
def run (self , host , module_name , module_args = None , ** kwargs ):
193
+ self .options .check = kwargs .get ("check" , False )
169
194
action = {"module" : module_name }
170
195
if module_args is not None :
171
196
if module_name in ("command" , "shell" ):
@@ -180,14 +205,13 @@ def run(self, host, module_name, module_args=None, **kwargs):
180
205
}],
181
206
}, variable_manager = self .variable_manager , loader = self .loader )
182
207
tqm = None
183
- options = Options (** kwargs )
184
208
callback = Callback ()
185
209
try :
186
210
tqm = ansible .executor .task_queue_manager .TaskQueueManager (
187
211
inventory = self .inventory ,
188
212
variable_manager = self .variable_manager ,
189
213
loader = self .loader ,
190
- options = options ,
214
+ options = self . options ,
191
215
passwords = None ,
192
216
stdout_callback = callback ,
193
217
)
@@ -208,16 +232,3 @@ def run(self, host, module_name, module_args=None, **kwargs):
208
232
raise NotImplementedError (
209
233
"Unhandled ansible version " + ansible .__version__
210
234
)
211
-
212
-
213
- def get_hosts (host_list = None , pattern = None ):
214
- return AnsibleRunner (host_list ).get_hosts (pattern )
215
-
216
-
217
- def run (host , module_name , module_args = None , host_list = None , ** kwargs ):
218
- return AnsibleRunner (host_list ).run (
219
- host , module_name , module_args , ** kwargs )
220
-
221
-
222
- def get_variables (host , host_list = None ):
223
- return AnsibleRunner (host_list ).get_variables (host )
0 commit comments