1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

only eval values if set from defaults

This commit is contained in:
Brian Coca 2017-09-11 20:22:16 -04:00 committed by Toshio Kuratomi
parent 6dbc3c63f8
commit a2b3bb1e49

View file

@ -14,6 +14,7 @@ from ansible.module_utils.parsing.convert_bool import boolean, BOOLEANS_TRUE
from ansible.module_utils.six import string_types
from ansible.config.manager import ConfigManager, ensure_type
def _deprecated(msg):
''' display is not guaranteed here, nor it being the full class, but try anyways, fallback to sys.stderr.write '''
try:
@ -23,11 +24,13 @@ def _deprecated(msg):
import sys
sys.stderr.write('[DEPRECATED] %s, to be removed in 2.8' % msg)
def mk_boolean(value):
''' moved to module_utils'''
_deprecated('ansible.constants.mk_boolean() is deprecated. Use ansible.module_utils.parsing.convert_bool.boolean() instead')
return boolean(value, strict=False)
def get_config(parser, section, key, env_var, default_value, value_type=None, expand_relative_paths=False):
''' kept for backwarsd compatibility, but deprecated '''
_deprecated('ansible.constants.get_config() is deprecated. There is new config API, see porting docs.')
@ -49,10 +52,12 @@ def get_config(parser, section, key, env_var, default_value, value_type=None, ex
return value
def set_constant(name, value, export=vars()):
''' sets constants and returns resolved options dict '''
export[name] = value
### CONSTANTS ### yes, actual ones
BLACKLIST_EXTS = ('.pyc', '.pyo', '.swp', '.bak', '~', '.rpm', '.md', '.txt')
BECOME_METHODS = ['sudo', 'su', 'pbrun', 'pfexec', 'doas', 'dzdo', 'ksu', 'runas', 'pmrun']
@ -99,15 +104,16 @@ config = ConfigManager()
# Generate constants from config
for setting in config.data.get_settings():
value = None
if isinstance(setting.value, string_types) and (setting.value.startswith('eval(') and setting.value.endswith(')')):
value = setting.value
if setting.origin == 'default' and \
isinstance(setting.value, string_types) and \
(setting.value.startswith('eval(') and setting.value.endswith(')')):
try:
# FIXME: find better way to do in manager class and/or ensure types
eval_string = setting.value.replace('eval(', '', 1)[:-1]
value = ensure_type(eval(eval_string), setting.type) # FIXME: safe eval?
except:
value = setting.value
# FIXME: should we warn?
pass
set_constant(setting.name, value or setting.value)