2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2016, Josh Bradley <jbradley(at)digitalocean.com>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2020-09-28 21:21:51 +02:00
|
|
|
author: Unknown (!UNKNOWN)
|
2021-01-12 07:12:03 +01:00
|
|
|
name: chef_databag
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: fetches data from a Chef Databag
|
|
|
|
description:
|
|
|
|
- "This is a lookup plugin to provide access to chef data bags using the pychef package.
|
|
|
|
It interfaces with the chef server api using the same methods to find a knife or chef-client config file to load parameters from,
|
|
|
|
starting from either the given base path or the current working directory.
|
|
|
|
The lookup order mirrors the one from Chef, all folders in the base path are walked back looking for the following configuration
|
|
|
|
file in order : .chef/knife.rb, ~/.chef/knife.rb, /etc/chef/client.rb"
|
|
|
|
requirements:
|
2022-06-22 13:36:23 +02:00
|
|
|
- "pychef (L(Python library, https://pychef.readthedocs.io), C(pip install pychef))"
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the databag
|
2024-07-21 21:03:41 +02:00
|
|
|
type: string
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
item:
|
|
|
|
description:
|
|
|
|
- Item to fetch
|
2024-07-21 21:03:41 +02:00
|
|
|
type: string
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2020-07-14 17:28:08 +02:00
|
|
|
- ansible.builtin.debug:
|
2020-08-08 22:04:34 +02:00
|
|
|
msg: "{{ lookup('community.general.chef_databag', 'name=data_bag_name item=data_bag_item') }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_raw:
|
|
|
|
description:
|
2020-09-16 11:06:45 +02:00
|
|
|
- The value from the databag.
|
|
|
|
type: list
|
|
|
|
elements: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from ansible.parsing.splitter import parse_kv
|
|
|
|
|
|
|
|
try:
|
|
|
|
import chef
|
|
|
|
HAS_CHEF = True
|
|
|
|
except ImportError as missing_module:
|
|
|
|
HAS_CHEF = False
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
"""
|
|
|
|
Chef data bag lookup module
|
|
|
|
"""
|
|
|
|
def __init__(self, loader=None, templar=None, **kwargs):
|
|
|
|
|
|
|
|
super(LookupModule, self).__init__(loader, templar, **kwargs)
|
|
|
|
|
|
|
|
# setup vars for data bag name and data bag item
|
|
|
|
self.name = None
|
|
|
|
self.item = None
|
|
|
|
|
|
|
|
def parse_kv_args(self, args):
|
|
|
|
"""
|
|
|
|
parse key-value style arguments
|
|
|
|
"""
|
|
|
|
|
|
|
|
for arg in ["name", "item"]:
|
|
|
|
try:
|
|
|
|
arg_raw = args.pop(arg, None)
|
|
|
|
if arg_raw is None:
|
|
|
|
continue
|
|
|
|
parsed = str(arg_raw)
|
|
|
|
setattr(self, arg, parsed)
|
|
|
|
except ValueError:
|
|
|
|
raise AnsibleError(
|
|
|
|
"can't parse arg {0}={1} as string".format(arg, arg_raw)
|
|
|
|
)
|
|
|
|
if args:
|
|
|
|
raise AnsibleError(
|
2021-02-23 23:19:27 +01:00
|
|
|
"unrecognized arguments to with_sequence: %r" % list(args.keys())
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, terms, variables=None, **kwargs):
|
|
|
|
# Ensure pychef has been loaded
|
|
|
|
if not HAS_CHEF:
|
|
|
|
raise AnsibleError('PyChef needed for lookup plugin, try `pip install pychef`')
|
|
|
|
|
|
|
|
for term in terms:
|
|
|
|
self.parse_kv_args(parse_kv(term))
|
|
|
|
|
|
|
|
api_object = chef.autoconfigure()
|
|
|
|
|
|
|
|
if not isinstance(api_object, chef.api.ChefAPI):
|
|
|
|
raise AnsibleError('Unable to connect to Chef Server API.')
|
|
|
|
|
|
|
|
data_bag_object = chef.DataBag(self.name)
|
|
|
|
|
|
|
|
data_bag_item = data_bag_object[self.item]
|
|
|
|
|
|
|
|
return [dict(data_bag_item)]
|