2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2017, Juan Manuel Parrilla <jparrill@redhat.com>
|
|
|
|
# Copyright (c) 2012-17 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 = '''
|
|
|
|
author:
|
|
|
|
- Juan Manuel Parrilla (@jparrill)
|
2021-01-12 07:12:03 +01:00
|
|
|
name: hiera
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: get info from hiera data
|
|
|
|
requirements:
|
|
|
|
- hiera (command line utility)
|
|
|
|
description:
|
2022-11-01 21:58:46 +01:00
|
|
|
- Retrieves data from an Puppetmaster node using Hiera as ENC.
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
2022-11-01 21:58:46 +01:00
|
|
|
_terms:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-11-01 21:58:46 +01:00
|
|
|
- The list of keys to lookup on the Puppetmaster.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: list
|
2020-09-16 11:06:45 +02:00
|
|
|
elements: string
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2022-11-01 21:58:46 +01:00
|
|
|
executable:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-11-01 21:58:46 +01:00
|
|
|
- Binary file to execute Hiera.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-03-09 10:11:07 +01:00
|
|
|
default: '/usr/bin/hiera'
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_HIERA_BIN
|
2022-11-01 21:58:46 +01:00
|
|
|
config_file:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-11-01 21:58:46 +01:00
|
|
|
- File that describes the hierarchy of Hiera.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-03-09 10:11:07 +01:00
|
|
|
default: '/etc/hiera.yaml'
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_HIERA_CFG
|
|
|
|
# FIXME: incomplete options .. _terms? environment/fqdn?
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
# All this examples depends on hiera.yml that describes the hierarchy
|
|
|
|
|
|
|
|
- name: "a value from Hiera 'DB'"
|
2020-08-08 22:04:34 +02:00
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ lookup('community.general.hiera', 'foo') }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "a value from a Hiera 'DB' on other environment"
|
2020-08-08 22:04:34 +02:00
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ lookup('community.general.hiera', 'foo environment=production') }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "a value from a Hiera 'DB' for a concrete node"
|
2020-08-08 22:04:34 +02:00
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ lookup('community.general.hiera', 'foo fqdn=puppet01.localdomain') }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_raw:
|
|
|
|
description:
|
|
|
|
- a value associated with input key
|
2020-09-16 11:06:45 +02:00
|
|
|
type: list
|
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from ansible.utils.cmd_functions import run_cmd
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Hiera(object):
|
2022-11-01 21:58:46 +01:00
|
|
|
def __init__(self, hiera_cfg, hiera_bin):
|
|
|
|
self.hiera_cfg = hiera_cfg
|
|
|
|
self.hiera_bin = hiera_bin
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def get(self, hiera_key):
|
2022-11-01 21:58:46 +01:00
|
|
|
pargs = [self.hiera_bin]
|
|
|
|
pargs.extend(['-c', self.hiera_cfg])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
pargs.extend(hiera_key)
|
|
|
|
|
|
|
|
rc, output, err = run_cmd("{0} -c {1} {2}".format(
|
2022-11-01 21:58:46 +01:00
|
|
|
self.hiera_bin, self.hiera_cfg, hiera_key[0]))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-04-24 12:20:11 +02:00
|
|
|
return to_text(output.strip())
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
2022-11-01 21:58:46 +01:00
|
|
|
def run(self, terms, variables=None, **kwargs):
|
|
|
|
self.set_options(var_options=variables, direct=kwargs)
|
|
|
|
|
|
|
|
hiera = Hiera(self.get_option('config_file'), self.get_option('executable'))
|
2021-04-05 09:22:06 +02:00
|
|
|
ret = [hiera.get(terms)]
|
2020-03-09 10:11:07 +01:00
|
|
|
return ret
|