HEX
Server: LiteSpeed
System: Linux s787.bom1.mysecurecloudhost.com 4.18.0-477.13.1.lve.el8.x86_64 #1 SMP Thu Jun 1 16:40:47 EDT 2023 x86_64
User: mobilech (5348)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/pillar/hiera.py
"""
Use hiera data as a Pillar source
"""

import logging

import salt.utils.path
import salt.utils.yaml

# Set up logging
log = logging.getLogger(__name__)


def __virtual__():
    """
    Only return if hiera is installed
    """
    return "hiera" if salt.utils.path.which("hiera") else False


def ext_pillar(
    minion_id, pillar, conf  # pylint: disable=W0613  # pylint: disable=W0613
):
    """
    Execute hiera and return the data
    """
    cmd = f"hiera -c {conf}"
    for key, val in __grains__.items():
        if isinstance(val, str):
            cmd += f" {key}='{val}'"
    try:
        data = salt.utils.yaml.safe_load(__salt__["cmd.run"](cmd))
    except Exception:  # pylint: disable=broad-except
        log.critical("Hiera YAML data failed to parse from conf %s", conf)
        return {}
    return data