config lookup, fallback to existing constants when needed (#86516)

devel
Brian Coca 2 days ago committed by GitHub
parent be09a21446
commit 8730acbb2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- config lookup now uses preexisting constants for templating when needed.

@ -2248,3 +2248,14 @@ _Z_TEST_ENTRY_2:
ini: ini:
- section: testing - section: testing
key: valid2 key: valid2
_Z_TEST_ENTRY_3:
version_added: '2.21'
name: testentry
description: for tests
type: path
default: '{{ANSIBLE_HOME}}'
env:
- name: ANSIBLE_TEST_ENTRY3
ini:
- section: testing
key: valid3

@ -80,6 +80,8 @@ _raw:
type: raw type: raw
""" """
from collections import ChainMap
import ansible.plugins.loader as plugin_loader import ansible.plugins.loader as plugin_loader
from ansible import constants as C from ansible import constants as C
@ -104,6 +106,9 @@ class LookupModule(LookupBase):
ret = [] ret = []
# primarily use task vars, but fallback to existing constants when needed
var_context = ChainMap(variables, vars(C))
for term in terms: for term in terms:
if not isinstance(term, str): if not isinstance(term, str):
raise AnsibleError(f'Invalid setting identifier, {term!r} is not a {str}, its a {type(term)}.') raise AnsibleError(f'Invalid setting identifier, {term!r} is not a {str}, its a {type(term)}.')
@ -119,7 +124,7 @@ class LookupModule(LookupBase):
if p is None: if p is None:
raise AnsibleError(f"Unable to load {ptype} plugin {pname!r}.") raise AnsibleError(f"Unable to load {ptype} plugin {pname!r}.")
try: try:
result, origin = C.config.get_config_value_and_origin(term, plugin_type=ptype, plugin_name=pname, variables=variables) result, origin = C.config.get_config_value_and_origin(term, plugin_type=ptype, plugin_name=pname, variables=var_context)
except AnsibleUndefinedConfigEntry as e: except AnsibleUndefinedConfigEntry as e:
match missing: match missing:
case 'error': case 'error':

@ -129,3 +129,8 @@
- config_origin1[1] == "default" - config_origin1[1] == "default"
- config_origin2[0] == 'yolo' - config_origin2[0] == 'yolo'
- 'config_origin2[1] == "var: _z_test_entry"' - 'config_origin2[1] == "var: _z_test_entry"'
- name: verify interdependent templating
assert:
that:
- lookup('config', 'ANSIBLE_HOME') == lookup('config', '_Z_TEST_ENTRY_3')
Loading…
Cancel
Save