ansible-infra/ansible_collections/community/sops/plugins/action/load_vars.py
chris e31ee5e39c
All checks were successful
/ build (pull_request) Successful in 28s
/ Ansible Lint (push) Successful in 2m36s
/ Ansible Lint (pull_request) Successful in 2m41s
update ansible collections sops, docker and debops
2026-09-21 22:46:50 +02:00

134 lines
4.8 KiB
Python

# Copyright (c) 2020, Felix Fontein <felix@fontein.de>
# 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
from __future__ import annotations
import typing as t
from collections.abc import Sequence, Mapping
from ansible.module_utils.common.text.converters import to_native
from ansible.utils.display import Display
from ansible_collections.community.sops.plugins.module_utils.sops import Sops, get_sops_argument_spec
from ansible_collections.community.sops.plugins.plugin_utils.action_module import ActionModuleBase, ArgumentSpec
try:
from ansible.template import trust_as_template as _trust_as_template
HAS_DATATAGGING = True
except ImportError:
HAS_DATATAGGING = False
try:
from ansible.plugins.action import VariableLayer # type: ignore[attr-defined]
HAS_REGISTER_HOST_VARIABLES = True
except ImportError:
HAS_REGISTER_HOST_VARIABLES = False
if t.TYPE_CHECKING:
from ansible_collections.community.sops.plugins.plugin_utils.action_module import AnsibleActionModule
display = Display()
def _make_safe(value: t.Any) -> t.Any:
if HAS_DATATAGGING and isinstance(value, str):
return _trust_as_template(value)
return value
class ActionModule(ActionModuleBase):
def _load(self, filename: str, module: AnsibleActionModule) -> dict:
def get_option_value(argument_name):
return module.params.get(argument_name)
output = Sops.decrypt(filename, display=display, get_option_value=get_option_value)
data = self._loader.load(output, file_name=filename, show_content=False)
if not data:
data = dict()
if not isinstance(data, dict):
# Should not happen with sops-encrypted files
raise Exception('{0} must be stored as a dictionary/hash'.format(to_native(filename)))
return data
def _evaluate(self, value):
if isinstance(value, str):
# must come *before* Sequence, as strings are also instances of Sequence
return self._templar.template(_make_safe(value))
if isinstance(value, Sequence):
return [self._evaluate(v) for v in value]
if isinstance(value, Mapping):
return dict((k, self._evaluate(v)) for k, v in value.items())
return value
def _make_safe(self, value):
if isinstance(value, str):
# must come *before* Sequence, as strings are also instances of Sequence
return _make_safe(value)
if isinstance(value, Sequence):
return [self._make_safe(v) for v in value]
if isinstance(value, Mapping):
return dict((k, self._make_safe(v)) for k, v in value.items())
return value
@staticmethod
def setup_module():
argument_spec = ArgumentSpec(
argument_spec=dict(
file=dict(type='path', required=True),
name=dict(type='str'),
expressions=dict(type='str', default='ignore', choices=['ignore', 'evaluate-on-load', 'lazy-evaluation']),
return_method=dict(type='str', default='auto', choices=['auto', 'facts-only', 'vars-only']),
),
)
argument_spec.argument_spec.update(get_sops_argument_spec())
return argument_spec, {}
def run_module(self, module):
expressions = module.params['expressions']
if expressions == 'lazy-evaluation' and not HAS_DATATAGGING:
module.fail_json(msg='expressions=lazy-evaluation requires ansible-core 2.19+ with Data Tagging support.')
return_method_str = module.params['return_method']
if return_method_str == 'auto':
return_as_facts = not HAS_REGISTER_HOST_VARIABLES
else:
return_as_facts = return_method_str == 'facts-only'
if not HAS_REGISTER_HOST_VARIABLES and not return_as_facts:
module.fail_json(msg='return_method=vars-only requires ansible-core 2.21+')
data = {}
files = []
try:
filename = self._find_needle('vars', module.params['file'])
data.update(self._load(filename, module))
files.append(filename)
except Exception as e:
module.fail_json(msg=to_native(e))
name = module.params['name']
if name is None:
value = data
else:
value = {name: data}
if expressions == 'evaluate-on-load':
value = self._evaluate(value)
if expressions == 'lazy-evaluation':
value = self._make_safe(value)
result = {
'ansible_included_var_files': files,
'_ansible_no_log': True,
}
if return_as_facts:
result['ansible_facts'] = value
else:
self.register_host_variables(variables=value, layer=VariableLayer.INCLUDE_VARS)
module.exit_json(**result)