| 1 |
# Copyright: (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# Copyright: (c) 2017, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
from string import ascii_letters, digits |
| 10 |
|
| 11 |
from ansible.config.manager import ConfigManager |
| 12 |
from ansible.module_utils.common.text.converters import to_text |
| 13 |
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE |
| 14 |
from ansible.utils.fqcn import add_internal_fqcns |
| 15 |
|
| 16 |
# initialize config manager/config data to read/store global settings |
| 17 |
# and generate 'pseudo constants' for app consumption. |
| 18 |
config = ConfigManager() |
| 19 |
|
| 20 |
|
| 21 |
def set_constant(name, value, export=vars()): |
| 22 |
""" sets constants and returns resolved options dict """ |
| 23 |
export[name] = value |
| 24 |
|
| 25 |
|
| 26 |
# CONSTANTS ### yes, actual ones |
| 27 |
|
| 28 |
# The following are hard-coded action names |
| 29 |
_ACTION_DEBUG = add_internal_fqcns(('debug', )) |
| 30 |
_ACTION_IMPORT_PLAYBOOK = add_internal_fqcns(('import_playbook', )) |
| 31 |
_ACTION_IMPORT_ROLE = add_internal_fqcns(('import_role', )) |
| 32 |
_ACTION_IMPORT_TASKS = add_internal_fqcns(('import_tasks', )) |
| 33 |
_ACTION_INCLUDE_ROLE = add_internal_fqcns(('include_role', )) |
| 34 |
_ACTION_INCLUDE_TASKS = add_internal_fqcns(('include_tasks', )) |
| 35 |
_ACTION_INCLUDE_VARS = add_internal_fqcns(('include_vars', )) |
| 36 |
_ACTION_META = add_internal_fqcns(('meta', )) |
| 37 |
_ACTION_SET_FACT = add_internal_fqcns(('set_fact', )) |
| 38 |
_ACTION_SETUP = add_internal_fqcns(('setup', )) |
| 39 |
_ACTION_HAS_CMD = add_internal_fqcns(('command', 'shell', 'script')) |
| 40 |
_ACTION_ALLOWS_RAW_ARGS = _ACTION_HAS_CMD + add_internal_fqcns(('raw', )) |
| 41 |
_ACTION_ALL_INCLUDES = _ACTION_INCLUDE_TASKS + _ACTION_INCLUDE_ROLE |
| 42 |
_ACTION_ALL_INCLUDE_IMPORT_TASKS = _ACTION_INCLUDE_TASKS + _ACTION_IMPORT_TASKS |
| 43 |
_ACTION_ALL_PROPER_INCLUDE_IMPORT_ROLES = _ACTION_INCLUDE_ROLE + _ACTION_IMPORT_ROLE |
| 44 |
_ACTION_ALL_PROPER_INCLUDE_IMPORT_TASKS = _ACTION_INCLUDE_TASKS + _ACTION_IMPORT_TASKS |
| 45 |
_ACTION_ALL_INCLUDE_ROLE_TASKS = _ACTION_INCLUDE_ROLE + _ACTION_INCLUDE_TASKS |
| 46 |
_ACTION_FACT_GATHERING = _ACTION_SETUP + add_internal_fqcns(('gather_facts', )) |
| 47 |
_ACTION_WITH_CLEAN_FACTS = _ACTION_SET_FACT + _ACTION_INCLUDE_VARS |
| 48 |
|
| 49 |
# http://nezzen.net/2008/06/23/colored-text-in-python-using-ansi-escape-sequences/ |
| 50 |
COLOR_CODES = { |
| 51 |
'black': u'0;30', 'bright gray': u'0;37', |
| 52 |
'blue': u'0;34', 'white': u'1;37', |
| 53 |
'green': u'0;32', 'bright blue': u'1;34', |
| 54 |
'cyan': u'0;36', 'bright green': u'1;32', |
| 55 |
'red': u'0;31', 'bright cyan': u'1;36', |
| 56 |
'purple': u'0;35', 'bright red': u'1;31', |
| 57 |
'yellow': u'0;33', 'bright purple': u'1;35', |
| 58 |
'dark gray': u'1;30', 'bright yellow': u'1;33', |
| 59 |
'magenta': u'0;35', 'bright magenta': u'1;35', |
| 60 |
'normal': u'0', |
| 61 |
} |
| 62 |
REJECT_EXTS = ['.pyc', '.pyo', '.swp', '.bak', '~', '.rpm', '.md', '.txt', '.rst'] # this is concatenated with other config settings as lists; cannot be tuple |
| 63 |
BOOL_TRUE = BOOLEANS_TRUE |
| 64 |
COLLECTION_PTYPE_COMPAT = {'module': 'modules'} |
| 65 |
|
| 66 |
PYTHON_DOC_EXTENSIONS = ('.py',) |
| 67 |
YAML_DOC_EXTENSIONS = ('.yml', '.yaml') |
| 68 |
DOC_EXTENSIONS = PYTHON_DOC_EXTENSIONS + YAML_DOC_EXTENSIONS |
| 69 |
|
| 70 |
DEFAULT_BECOME_PASS = None |
| 71 |
DEFAULT_PASSWORD_CHARS = to_text(ascii_letters + digits + ".,:-_", errors='strict') # characters included in auto-generated passwords |
| 72 |
DEFAULT_REMOTE_PASS = None |
| 73 |
DEFAULT_SUBSET = None |
| 74 |
# FIXME: expand to other plugins, but never doc fragments |
| 75 |
CONFIGURABLE_PLUGINS = ('become', 'cache', 'callback', 'cliconf', 'connection', 'httpapi', 'inventory', 'lookup', 'netconf', 'shell', 'vars') |
| 76 |
# NOTE: always update the docs/docsite/Makefile to match |
| 77 |
DOCUMENTABLE_PLUGINS = CONFIGURABLE_PLUGINS + ('module', 'strategy', 'test', 'filter') |
| 78 |
IGNORE_FILES = ("COPYING", "CONTRIBUTING", "LICENSE", "README", "VERSION", "GUIDELINES", "MANIFEST", "Makefile") # ignore during module search |
| 79 |
INTERNAL_STATIC_VARS = frozenset( |
| 80 |
[ |
| 81 |
"ansible_async_path", |
| 82 |
"ansible_collection_name", |
| 83 |
"ansible_config_file", |
| 84 |
"ansible_dependent_role_names", |
| 85 |
"ansible_diff_mode", |
| 86 |
"ansible_config_file", |
| 87 |
"ansible_facts", |
| 88 |
"ansible_forks", |
| 89 |
"ansible_inventory_sources", |
| 90 |
"ansible_limit", |
| 91 |
"ansible_play_batch", |
| 92 |
"ansible_play_hosts", |
| 93 |
"ansible_play_hosts_all", |
| 94 |
"ansible_play_role_names", |
| 95 |
"ansible_playbook_python", |
| 96 |
"ansible_role_name", |
| 97 |
"ansible_role_names", |
| 98 |
"ansible_run_tags", |
| 99 |
"ansible_skip_tags", |
| 100 |
"ansible_verbosity", |
| 101 |
"ansible_version", |
| 102 |
"inventory_dir", |
| 103 |
"inventory_file", |
| 104 |
"inventory_hostname", |
| 105 |
"inventory_hostname_short", |
| 106 |
"groups", |
| 107 |
"group_names", |
| 108 |
"hostvars", |
| 109 |
"playbook_dir", |
| 110 |
"play_hosts", |
| 111 |
"role_name", |
| 112 |
"role_names", |
| 113 |
"role_path", |
| 114 |
"role_uuid", |
| 115 |
"role_names", |
| 116 |
] |
| 117 |
) |
| 118 |
LOCALHOST = ('127.0.0.1', 'localhost', '::1') |
| 119 |
WIN_MOVED = ['ansible.windows.win_command', 'ansible.windows.win_shell'] |
| 120 |
MODULE_REQUIRE_ARGS_SIMPLE = ['command', 'raw', 'script', 'shell', 'win_command', 'win_shell'] |
| 121 |
MODULE_REQUIRE_ARGS = tuple(add_internal_fqcns(MODULE_REQUIRE_ARGS_SIMPLE) + WIN_MOVED) |
| 122 |
MODULE_NO_JSON = tuple(add_internal_fqcns(('command', 'win_command', 'shell', 'win_shell', 'raw')) + WIN_MOVED) |
| 123 |
RESTRICTED_RESULT_KEYS = ('ansible_playbook_python', 'ansible_facts') |
| 124 |
SYNTHETIC_COLLECTIONS = ('ansible.builtin', 'ansible.legacy') |
| 125 |
TREE_DIR = None |
| 126 |
VAULT_VERSION_MIN = 1.0 |
| 127 |
VAULT_VERSION_MAX = 1.0 |
| 128 |
|
| 129 |
# This matches a string that cannot be used as a valid python variable name i.e 'not-valid', 'not!valid@either' '1_nor_This' |
| 130 |
INVALID_VARIABLE_NAMES = re.compile(r'^[\d\W]|[^\w]') |
| 131 |
|
| 132 |
|
| 133 |
# FIXME: remove once play_context mangling is removed |
| 134 |
# the magic variable mapping dictionary below is used to translate |
| 135 |
# host/inventory variables to fields in the PlayContext |
| 136 |
# object. The dictionary values are tuples, to account for aliases |
| 137 |
# in variable names. |
| 138 |
|
| 139 |
COMMON_CONNECTION_VARS = frozenset(('ansible_connection', 'ansible_host', 'ansible_user', 'ansible_shell_executable', |
| 140 |
'ansible_port', 'ansible_pipelining', 'ansible_password', 'ansible_timeout', |
| 141 |
'ansible_shell_type', 'ansible_module_compression', 'ansible_private_key_file')) |
| 142 |
|
| 143 |
MAGIC_VARIABLE_MAPPING = dict( |
| 144 |
|
| 145 |
# base |
| 146 |
connection=('ansible_connection', ), |
| 147 |
module_compression=('ansible_module_compression', ), |
| 148 |
shell=('ansible_shell_type', ), |
| 149 |
executable=('ansible_shell_executable', ), |
| 150 |
|
| 151 |
# connection common |
| 152 |
remote_addr=('ansible_ssh_host', 'ansible_host'), |
| 153 |
remote_user=('ansible_ssh_user', 'ansible_user'), |
| 154 |
password=('ansible_ssh_pass', 'ansible_password'), |
| 155 |
port=('ansible_ssh_port', 'ansible_port'), |
| 156 |
pipelining=('ansible_ssh_pipelining', 'ansible_pipelining'), |
| 157 |
timeout=('ansible_ssh_timeout', 'ansible_timeout'), |
| 158 |
private_key_file=('ansible_ssh_private_key_file', 'ansible_private_key_file'), |
| 159 |
|
| 160 |
# networking modules |
| 161 |
network_os=('ansible_network_os', ), |
| 162 |
connection_user=('ansible_connection_user',), |
| 163 |
|
| 164 |
# ssh TODO: remove |
| 165 |
ssh_executable=('ansible_ssh_executable', ), |
| 166 |
ssh_common_args=('ansible_ssh_common_args', ), |
| 167 |
sftp_extra_args=('ansible_sftp_extra_args', ), |
| 168 |
scp_extra_args=('ansible_scp_extra_args', ), |
| 169 |
ssh_extra_args=('ansible_ssh_extra_args', ), |
| 170 |
ssh_transfer_method=('ansible_ssh_transfer_method', ), |
| 171 |
|
| 172 |
# docker TODO: remove |
| 173 |
docker_extra_args=('ansible_docker_extra_args', ), |
| 174 |
|
| 175 |
# become |
| 176 |
become=('ansible_become', ), |
| 177 |
become_method=('ansible_become_method', ), |
| 178 |
become_user=('ansible_become_user', ), |
| 179 |
become_pass=('ansible_become_password', 'ansible_become_pass'), |
| 180 |
become_exe=('ansible_become_exe', ), |
| 181 |
become_flags=('ansible_become_flags', ), |
| 182 |
) |
| 183 |
|
| 184 |
# POPULATE SETTINGS FROM CONFIG ### |
| 185 |
for setting in config.get_configuration_definitions(): |
| 186 |
set_constant(setting, config.get_config_value(setting, variables=vars())) |