From b8907ff09efeb8601fdfcd4d2e91fc562e92da58 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 28 Aug 2018 12:51:26 +1000 Subject: [PATCH] k8s_facts should not throw exceptions when not found (#44429) Handle the case where a resource is not found by catching the exception and returning an empty result set. --- lib/ansible/module_utils/k8s/common.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index edb8fa3aab4..95839daabfe 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -205,10 +205,14 @@ class K8sAnsibleMixin(object): def kubernetes_facts(self, kind, api_version, name=None, namespace=None, label_selectors=None, field_selectors=None): resource = self.find_resource(kind, api_version) - result = resource.get(name=name, - namespace=namespace, - label_selector=','.join(label_selectors), - field_selector=','.join(field_selectors)).to_dict() + try: + result = resource.get(name=name, + namespace=namespace, + label_selector=','.join(label_selectors), + field_selector=','.join(field_selectors)).to_dict() + except openshift.dynamic.exceptions.NotFoundError: + return dict(items=[]) + if 'items' in result: return result else: