67
67
default: false
68
68
only_include_active:
69
69
description:
70
- - Onlyincludes hosts with active=active
70
+ - Only includes hosts with active=active
71
71
type: bool
72
72
default: true
73
73
dog_url:
110
110
type: list
111
111
default: []
112
112
request_timeout:
113
- version_added: "1.0.4"
114
- description:
115
- - Request timeout in seconds to dog API
116
- ini:
117
- - {key: request_timeout, section: dog_connection}
118
- type: float
119
- default: 300.0
113
+ version_added: "1.0.4"
114
+ description:
115
+ - Request timeout in seconds to dog API
116
+ ini:
117
+ - {key: request_timeout, section: dog_connection}
118
+ type: float
119
+ default: 300.0
120
120
"""
121
121
122
122
EXAMPLES = """
149
149
150
150
MIN_DOG_API = "V2"
151
151
152
- def list_to_dict (lst ):
153
- res_dict = {}
154
- for i in range (0 , len (lst )):
155
- res_dict [lst [i ].get ("name" )] = lst [i ]
156
- return res_dict
157
152
158
153
class InventoryModule (BaseInventoryPlugin , Constructable ):
159
154
"""Host inventory parser for ansible using dog as source."""
@@ -178,12 +173,10 @@ def _populate(self, client):
178
173
hosts = client .get_all_hosts ()
179
174
except Exception as exc :
180
175
raise AnsibleError ("Error listing containers: %s" % to_native (exc ))
181
- hosts_dict = list_to_dict (hosts )
182
- #print(hosts_dict)
183
176
184
177
try :
185
- dog_groups_list = client .get_all_groups ()
186
178
dog_groups = {}
179
+ dog_groups_list = client .get_all_groups ()
187
180
for group in dog_groups_list :
188
181
group_name = self .fix_group (group .get ("name" ))
189
182
dog_groups [group_name ] = group
@@ -195,29 +188,24 @@ def _populate(self, client):
195
188
fact_groups_dict = fact .get ("groups" )
196
189
fact_groups = {}
197
190
for group_name , group in fact_groups_dict .items ():
198
- fact_hosts = group .get ("hosts" )
199
- group ["hosts" ] = {}
200
- #only include facts for hosts that exist
201
- for key ,values in fact_hosts .items ():
202
- #print(key)
203
- if hosts_dict .get (key ):
204
- group ["hosts" ][key ] = values
205
- #print(group)
206
191
group_name = self .fix_group (group_name )
192
+ group_hosts = group .get ("hosts" )
193
+ #only add fact hosts that are active hosts
194
+ new_group_hosts = {}
195
+ for group_host , group_host_values in group_hosts .items ():
196
+ if group_host in hosts :
197
+ new_group_hosts [group_host ] = group_host_values
198
+ group ["hosts" ] = new_group_hosts
207
199
fact_groups [group_name ] = group
208
200
self .groups = always_merger .merge (fact_groups , dog_groups )
209
- #self.groups = dog_groups
210
201
except ClientError :
211
202
print (f'WARNING: dog_fact "{ self .dog_fact } " not found' )
212
203
self .groups = dog_groups
213
204
214
-
215
205
for group_name , group in self .groups .items ():
216
206
self .parse_group (group_name , group )
217
207
218
208
for host in hosts :
219
- #active = host.get("active")
220
-
221
209
break_flag = False
222
210
for filter in self .filters :
223
211
key = filter .get ("key" )
@@ -235,9 +223,6 @@ def _populate(self, client):
235
223
if break_flag is True :
236
224
continue
237
225
238
- #if only_include_active is True:
239
- # if active != "active":
240
- # continue
241
226
self .parse_host (host )
242
227
243
228
def parse_host (self , host ):
0 commit comments