aboutsummaryrefslogtreecommitdiff
path: root/foreman_ansible_inventory.py
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-04-29 21:33:28 +0200
committerGuido Günther <agx@sigxcpu.org>2016-04-29 22:04:06 +0200
commit87fe095adc1f3c66bb061a0335ede4550725f1b6 (patch)
tree70a01b4afb360953cb5098380d0effba09fec597 /foreman_ansible_inventory.py
parent4cb6d186a27a86da6dd20796e1c1efc6673e0dbf (diff)
Don't fail if host disappears
A host might disappear once we query its params. Don't fail like Traceback (most recent call last): File "inventory", line 295, in <module> ForemanInventory() File "inventory", line 52, in __init__ self.update_cache() File "inventory", line 204, in update_cache params = self._resolve_params(host) File "inventory", line 180, in _resolve_params paramgroups += [self._get_params_by_id(host['id'])] File "inventory", line 158, in _get_params_by_id return self._get_json(url) File "inventory", line 137, in _get_json ret.raise_for_status() File "models.py", line 840, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https:parameters?per_page=250&page=1> in this case. We need to do this for disappearing host groups, locations and organizations as well at a later point but VMs disappaer much more frequently..
Diffstat (limited to 'foreman_ansible_inventory.py')
-rwxr-xr-xforeman_ansible_inventory.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py
index bece7c7..57cf50a 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -133,7 +133,7 @@ class ForemanInventory(object):
help='Force refresh of cache by making API requests to foreman (default: False - use cache files)')
self.args = parser.parse_args()
- def _get_json(self, url):
+ def _get_json(self, url, ignore_errors=None):
page = 1
results = []
while True:
@@ -141,6 +141,8 @@ class ForemanInventory(object):
auth=HTTPBasicAuth(self.foreman_user, self.foreman_pw),
verify=self.foreman_ssl_verify,
params={'page': page, 'per_page': 250})
+ if ignore_errors and ret.status_code in ignore_errors:
+ break
ret.raise_for_status()
json = ret.json()
if not json.has_key('results'):
@@ -162,7 +164,7 @@ class ForemanInventory(object):
def _get_params_by_id(self, hid):
url = "%s/api/v2/hosts/%s/parameters" % (self.foreman_url, hid)
- return self._get_json(url)
+ return self._get_json(url, [404])
def _resolve_params(self, host):
"""