179 lines
5.0 KiB
Python
179 lines
5.0 KiB
Python
# Copyright 2021 Robin Cordier <robin.cordier@bell.ca>
|
|
# Copyright 2017 Craig Dunn <craig@craigdunn.org>
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import requests
|
|
import yaml
|
|
|
|
|
|
from copy import deepcopy
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.lookup import LookupBase
|
|
from ansible.template import generate_ansible_template_vars, AnsibleEnvironment, USE_JINJA2_NATIVE
|
|
|
|
from ansible_collections.barbu_it.ansible_kheops.plugins.plugin_utils.common import DOCUMENTATION_OPTION_FRAGMENT, AnsibleKheops
|
|
|
|
|
|
try:
|
|
from __main__ import display
|
|
except ImportError:
|
|
from ansible.utils.display import Display
|
|
display = Display()
|
|
|
|
if USE_JINJA2_NATIVE:
|
|
from ansible.utils.native_jinja import NativeJinjaText
|
|
|
|
DOCUMENTATION = """
|
|
lookup: kheops
|
|
author: Robin Cordier <robin.cordier@bell.ca>
|
|
version_added: "3"
|
|
short_description: read key/values from Kheops
|
|
description:
|
|
- This lookup returns the contents of a Kheops key
|
|
- This is an improved fork of https://github.com/kheops/kheops-ansible-lookup-plugin
|
|
- This fork provides python3 support and more query options
|
|
options:
|
|
_terms:
|
|
description: One or more string terms prefixed by a namespace. Format is `<namespace>/<key>`.
|
|
required: True
|
|
|
|
""" + DOCUMENTATION_OPTION_FRAGMENT
|
|
|
|
EXAMPLES = """
|
|
- name: Return the value of key
|
|
debug:
|
|
msg: "{{ lookup('kheops', 'default/key') }}"
|
|
|
|
- name: Return a list of values
|
|
debug:
|
|
msg: "{{ lookup('kheops', 'ansible/yum_packages', 'ansible/yum_repos') }}"
|
|
|
|
- name: Advanced usage
|
|
debug:
|
|
msg: "{{ lookup('kheops', 'ansible/yum_packages', merge='deep_hash', lookup_type='cascade') }}"
|
|
|
|
- name: Advanced usage with custom parameters
|
|
debug:
|
|
msg: "{{ lookup('kheops', 'ansible/yum_packages', policy='ansible') }}"
|
|
|
|
"""
|
|
|
|
RETURN = """
|
|
_data:
|
|
description:
|
|
- Value of the key, when only one term is searched
|
|
_list:
|
|
description:
|
|
- List of value of the keys, when more than one term is searched
|
|
type: list
|
|
|
|
"""
|
|
|
|
from pprint import pprint
|
|
|
|
# Entry point for Ansible starts here with the LookupModule class
|
|
class LookupModule(LookupBase):
|
|
|
|
|
|
def run(self, terms, variables=None, scope=None, **kwargs):
|
|
|
|
self.set_options(direct=kwargs)
|
|
|
|
process_scope = self.get_option('process_scope')
|
|
process_results = self.get_option('process_results')
|
|
jinja2_native = kwargs.pop('jinja2_native', self.get_option('jinja2_native'))
|
|
|
|
|
|
|
|
# Prepare Kheops instance
|
|
configs = [
|
|
self.get_option('config'),
|
|
kwargs,
|
|
#{
|
|
# "instance_log_level": 'DEBUG',
|
|
# }
|
|
]
|
|
kheops = AnsibleKheops(configs=configs, display=self._display)
|
|
|
|
|
|
# Start jinja template engine
|
|
if USE_JINJA2_NATIVE and not jinja2_native:
|
|
templar = self._templar.copy_with_new_env(environment_class=AnsibleEnvironment)
|
|
else:
|
|
templar = self._templar
|
|
|
|
# Transform dict to list for lookup/queries
|
|
ret = []
|
|
#for term in terms:
|
|
|
|
results = kheops.super_lookup_seq(
|
|
keys=terms,
|
|
#scope=scope,
|
|
scope_vars=variables,
|
|
_templar=templar,
|
|
jinja2_native=jinja2_native,
|
|
_process_results='jinja',
|
|
)
|
|
|
|
final = []
|
|
for result in results:
|
|
only_key = list(result.keys())
|
|
if len(only_key) > 0:
|
|
final.append(result[only_key[0]])
|
|
|
|
return final
|
|
|
|
try:
|
|
found_term = list(result[0].keys())
|
|
if len(found_term) > 0:
|
|
return result[0][found_term[0]]
|
|
except Exception as err:
|
|
print (err)
|
|
return result[0]
|
|
|
|
|
|
return None
|
|
|
|
# Merge results if any
|
|
#from pprint import pprint
|
|
#pprint (result)
|
|
final = None
|
|
if len(result) > 0:
|
|
final_keys = list(result[0].keys())
|
|
#print ("Final key", final_keys)
|
|
if len(final_keys) > 0:
|
|
#print ("yeahhhh", final_keys[0], result[0][final_keys[0]])
|
|
final = result[0][final_keys[0]]
|
|
if len(final_keys) > 1:
|
|
print ("Warning, other results are ignored !!!")
|
|
|
|
return final
|
|
|
|
|
|
#final = result[0].keys()[0] if len(result) > 0 else None
|
|
|
|
#final = {}
|
|
#for res in result:
|
|
# pprint (res)
|
|
# final.update(res)
|
|
|
|
#final = result
|
|
|
|
ret.append(final)
|
|
|
|
return ret
|