Skip to content

Commit 6640306

Browse files
committed
static hosts described in fact table now only effect inventory when a matching dog host is found
1 parent 425ef77 commit 6640306

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

plugins/inventory/dog_inventory.py

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
default: false
6868
only_include_active:
6969
description:
70-
- Onlyincludes hosts with active=active
70+
- Only includes hosts with active=active
7171
type: bool
7272
default: true
7373
dog_url:
@@ -110,13 +110,13 @@
110110
type: list
111111
default: []
112112
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
120120
"""
121121

122122
EXAMPLES = """
@@ -149,11 +149,6 @@
149149

150150
MIN_DOG_API = "V2"
151151

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
157152

158153
class InventoryModule(BaseInventoryPlugin, Constructable):
159154
"""Host inventory parser for ansible using dog as source."""
@@ -178,12 +173,10 @@ def _populate(self, client):
178173
hosts = client.get_all_hosts()
179174
except Exception as exc:
180175
raise AnsibleError("Error listing containers: %s" % to_native(exc))
181-
hosts_dict = list_to_dict(hosts)
182-
#print(hosts_dict)
183176

184177
try:
185-
dog_groups_list = client.get_all_groups()
186178
dog_groups = {}
179+
dog_groups_list = client.get_all_groups()
187180
for group in dog_groups_list:
188181
group_name = self.fix_group(group.get("name"))
189182
dog_groups[group_name] = group
@@ -195,29 +188,24 @@ def _populate(self, client):
195188
fact_groups_dict = fact.get("groups")
196189
fact_groups = {}
197190
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)
206191
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
207199
fact_groups[group_name] = group
208200
self.groups = always_merger.merge(fact_groups, dog_groups)
209-
#self.groups = dog_groups
210201
except ClientError:
211202
print(f'WARNING: dog_fact "{self.dog_fact}" not found')
212203
self.groups = dog_groups
213204

214-
215205
for group_name, group in self.groups.items():
216206
self.parse_group(group_name, group)
217207

218208
for host in hosts:
219-
#active = host.get("active")
220-
221209
break_flag = False
222210
for filter in self.filters:
223211
key = filter.get("key")
@@ -235,9 +223,6 @@ def _populate(self, client):
235223
if break_flag is True:
236224
continue
237225

238-
#if only_include_active is True:
239-
# if active != "active":
240-
# continue
241226
self.parse_host(host)
242227

243228
def parse_host(self, host):

0 commit comments

Comments
 (0)