Skip to content

Commit ab7b6d5

Browse files
committed
fixed filters so plugin won't include hosts where filter values are undefined
1 parent f0b522e commit ab7b6d5

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

import_inventory_to_fact.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def main(argv, stdout, environ):
3838
"groups": groups}
3939
with open('fact.import.json', 'w') as output_file:
4040
output_file.write(json.dumps(dog_fact))
41-
# http POST https://$DOG:8443/api/V2/fact @fact.import.json -A bearer -a $TOKEN
41+
#Then add to dog:
42+
# curl -H 'Content-Type: application/json' -H "Authorization: Bearer $TF_VAR_dog_api_token_qa" -X POST https://dog-qa.relaydev.sh:8443/api/V2/fact --data @fact.import.json
4243

4344

4445
if __name__ == "__main__":

plugins/inventory/dog_inventory.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,28 @@ def _populate(self, client):
223223
#sys.exit()
224224
for host in hosts:
225225
break_flag = False
226-
for filter in self.filters:
227-
key = filter.get("key")
228-
expected_value = filter.get("value")
226+
for filter_config in self.filters:
227+
key = filter_config.get("key")
228+
expected_value = filter_config.get("value")
229229
try:
230-
value = self._compose(key, host)
231-
if value != expected_value:
230+
actual_value = self._compose(key, host)
231+
# Key is defined, and we have its actual_value
232+
if actual_value != expected_value:
233+
break_flag = True # Value mismatch, so host fails this filter
234+
break
235+
# If actual_value == expected_value, this filter is matched. Continue to next filter.
236+
except (jinja2.exceptions.UndefinedError, ansible.errors.AnsibleUndefinedVariable):
237+
# Key is undefined in the host data.
238+
# Check if the filter expected it to be undefined.
239+
# We assume 'None' (from YAML null) in the filter's 'value' means "should be undefined".
240+
if expected_value is None:
241+
# Filter expects key to be undefined, and it is. This filter is matched.
242+
# Continue to the next filter for this host.
243+
pass
244+
else:
245+
# Filter expects a concrete value, but key is undefined. This filter is NOT matched.
232246
break_flag = True
233247
break
234-
except jinja2.exceptions.UndefinedError:
235-
break
236-
except ansible.errors.AnsibleUndefinedVariable:
237-
break
238248

239249
if break_flag is True:
240250
continue

0 commit comments

Comments
 (0)