From b277f850d891c175779740632b9063e47030722b Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 3 Jun 2020 13:36:36 +0530 Subject: [PATCH] Handle set object retrieved from lookup plugin self.client.resources.api_groups is a dict_keys and is not handled correctly by default callback plugin while JSON serialization. This fix will typecast it to list so that it can be JSON serialized for further processing. Fixes: #111 Signed-off-by: Abhijeet Kasurde --- molecule/default/tasks/full.yml | 7 +++++++ plugins/lookup/k8s.py | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/molecule/default/tasks/full.yml b/molecule/default/tasks/full.yml index 85d5d7d4..d2666797 100644 --- a/molecule/default/tasks/full.yml +++ b/molecule/default/tasks/full.yml @@ -185,6 +185,13 @@ metadata: name: testing1 + ### https://github.com/ansible-collections/community.kubernetes/issues/111 + - set_fact: + api_groups: "{{ lookup('k8s', cluster_info='api_groups') }}" + + - debug: + var: api_groups + - name: Namespace should exist k8s_info: kind: Namespace diff --git a/plugins/lookup/k8s.py b/plugins/lookup/k8s.py index 314c52c0..bcedc976 100644 --- a/plugins/lookup/k8s.py +++ b/plugins/lookup/k8s.py @@ -194,12 +194,12 @@ type: complex """ +from ansible.errors import AnsibleError +from ansible.module_utils.common._collections_compat import KeysView from ansible.plugins.lookup import LookupBase from ansible_collections.community.kubernetes.plugins.module_utils.common import K8sAnsibleMixin -from ansible.errors import AnsibleError - try: from openshift.dynamic import DynamicClient @@ -253,6 +253,8 @@ def run(self, terms, variables=None, **kwargs): if cluster_info == 'version': return [self.client.version] if cluster_info == 'api_groups': + if isinstance(self.client.resources.api_groups, KeysView): + return [list(self.client.resources.api_groups)] return [self.client.resources.api_groups] self.kind = kwargs.get('kind')