Skip to content

Commit 88cf936

Browse files
committed
Allow to load vars with expressions evaluated lazily.
1 parent 82338f3 commit 88cf936

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

plugins/action/load_vars.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,33 @@ def _evaluate(self, value):
5555
return dict((k, self._evaluate(v)) for k, v in iteritems(value))
5656
return value
5757

58+
def _make_safe(self, value):
59+
if isinstance(value, string_types):
60+
# must come *before* Sequence, as strings are also instances of Sequence
61+
return _make_safe(value)
62+
if isinstance(value, Sequence):
63+
return [self._make_safe(v) for v in value]
64+
if isinstance(value, Mapping):
65+
return dict((k, self._make_safe(v)) for k, v in iteritems(value))
66+
return value
67+
5868
@staticmethod
5969
def setup_module():
6070
argument_spec = ArgumentSpec(
6171
argument_spec=dict(
6272
file=dict(type='path', required=True),
6373
name=dict(type='str'),
64-
expressions=dict(type='str', default='ignore', choices=['ignore', 'evaluate-on-load']),
74+
expressions=dict(type='str', default='ignore', choices=['ignore', 'evaluate-on-load', 'lazy-evaluation']),
6575
),
6676
)
6777
argument_spec.argument_spec.update(get_sops_argument_spec())
6878
return argument_spec, {}
6979

7080
def run_module(self, module):
81+
expressions = module.params['expressions']
82+
if expressions == 'lazy-evaluation' and not HAS_DATATAGGING:
83+
module.fail_json(msg='expressions=lazy-evaluation requires ansible-core 2.19+ with Data Tagging support.')
84+
7185
data = dict()
7286
files = []
7387
try:
@@ -84,10 +98,12 @@ def run_module(self, module):
8498
value = dict()
8599
value[name] = data
86100

87-
expressions = module.params['expressions']
88101
if expressions == 'evaluate-on-load':
89102
value = self._evaluate(value)
90103

104+
if expressions == 'lazy-evaluation':
105+
value = self._make_safe(value)
106+
91107
module.exit_json(
92108
ansible_included_var_files=files,
93109
ansible_facts=value,

plugins/modules/load_vars.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
- If set to V(ignore), expressions will not be evaluated, but treated as regular strings.
3535
- If set to V(evaluate-on-load), expressions will be evaluated on execution of this module, in other words, when the
3636
file is loaded.
37-
- Unfortunately, there is no way for non-core modules to handle expressions "unsafe", in other words, evaluate them
38-
only on use. This can only achieved by M(ansible.builtin.include_vars), which unfortunately cannot handle SOPS-encrypted
39-
files.
37+
- If set to V(lazy-evaluation), expressions will be lazily evaluated. This requires ansible-core 2.19 or newer
38+
and is the same behavior than M(ansible.builtin.include_vars). V(lazy-evaluation) has been added in community.sops 2.1.0.
4039
type: str
4140
default: ignore
4241
choices:
4342
- ignore
4443
- evaluate-on-load
44+
- lazy-evaluation
4545
extends_documentation_fragment:
4646
- community.sops.sops
4747
- community.sops.attributes

tests/integration/targets/load_vars/tasks/main.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
- assert:
2525
that:
26-
- '"value of expressions must be one of: ignore, evaluate-on-load, got: invalid value" in load_vars_invalid_value.msg'
26+
- '"value of expressions must be one of: ignore, evaluate-on-load, lazy-evaluation, got: invalid value" in load_vars_invalid_value.msg'
2727

2828
- name: Test load_vars with missing file
2929
community.sops.load_vars:
@@ -124,3 +124,35 @@
124124
that:
125125
- load_vars_expr_evaluated_now_2 is success
126126
- test2_2 == 'something_else'
127+
128+
- when: ansible_version.full is version('2.19', '>=')
129+
block:
130+
- set_fact:
131+
to_be_defined_earlier: something_defined_before
132+
bar_2: baz
133+
134+
- name: Test load_vars with expressions evaluated lazily
135+
community.sops.load_vars:
136+
file: proper-vars-2.sops.yaml
137+
expressions: lazy-evaluation
138+
register: load_vars_expr_lazy_evaluated
139+
140+
- assert:
141+
that:
142+
- load_vars_expr_lazy_evaluated is success
143+
- test1_2 == 'baz'
144+
- test2_2 == 'something_defined_before'
145+
- test3_2[0] == 'baz'
146+
- test4_2.test_4_2_1 == 'bazbaz'
147+
148+
- set_fact:
149+
to_be_defined_earlier: something_else
150+
bar_2: buzz
151+
152+
- assert:
153+
that:
154+
- load_vars_expr_lazy_evaluated is success
155+
- test1_2 == 'buzz'
156+
- test2_2 == 'something_else'
157+
- test3_2[0] == 'buzz'
158+
- test4_2.test_4_2_1 == 'buzzbuzz'

0 commit comments

Comments
 (0)