| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 4 |
# |
| 5 |
# This file is part of Ansible |
| 6 |
# |
| 7 |
# Ansible is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Ansible is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
from __future__ import annotations |
| 21 |
|
| 22 |
from ansible import constants as C |
| 23 |
from ansible import context |
| 24 |
from ansible.playbook.attribute import FieldAttribute |
| 25 |
from ansible.playbook.base import Base |
| 26 |
from ansible.utils.display import Display |
| 27 |
|
| 28 |
|
| 29 |
display = Display() |
| 30 |
|
| 31 |
|
| 32 |
__all__ = ['PlayContext'] |
| 33 |
|
| 34 |
|
| 35 |
TASK_ATTRIBUTE_OVERRIDES = ( |
| 36 |
'become', |
| 37 |
'become_user', |
| 38 |
'become_pass', |
| 39 |
'become_method', |
| 40 |
'become_flags', |
| 41 |
'connection', |
| 42 |
'docker_extra_args', # TODO: remove |
| 43 |
'delegate_to', |
| 44 |
'no_log', |
| 45 |
'remote_user', |
| 46 |
) |
| 47 |
|
| 48 |
RESET_VARS = ( |
| 49 |
'ansible_connection', |
| 50 |
'ansible_user', |
| 51 |
'ansible_host', |
| 52 |
'ansible_port', |
| 53 |
|
| 54 |
# TODO: ??? |
| 55 |
'ansible_docker_extra_args', |
| 56 |
'ansible_ssh_host', |
| 57 |
'ansible_ssh_pass', |
| 58 |
'ansible_ssh_port', |
| 59 |
'ansible_ssh_user', |
| 60 |
'ansible_ssh_private_key_file', |
| 61 |
'ansible_ssh_pipelining', |
| 62 |
'ansible_ssh_executable', |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
class PlayContext(Base): |
| 67 |
|
| 68 |
""" |
| 69 |
This class is used to consolidate the connection information for |
| 70 |
hosts in a play and child tasks, where the task may override some |
| 71 |
connection/authentication information. |
| 72 |
""" |
| 73 |
|
| 74 |
_post_validate_object = True |
| 75 |
|
| 76 |
# base |
| 77 |
module_compression = FieldAttribute(isa='string', default=C.DEFAULT_MODULE_COMPRESSION) |
| 78 |
shell = FieldAttribute(isa='string') |
| 79 |
executable = FieldAttribute(isa='string', default=C.DEFAULT_EXECUTABLE) |
| 80 |
|
| 81 |
# connection fields, some are inherited from Base: |
| 82 |
# (connection, port, remote_user, environment, no_log) |
| 83 |
remote_addr = FieldAttribute(isa='string') |
| 84 |
password = FieldAttribute(isa='string') |
| 85 |
timeout = FieldAttribute(isa='int', default=C.DEFAULT_TIMEOUT) |
| 86 |
connection_user = FieldAttribute(isa='string') |
| 87 |
private_key_file = FieldAttribute(isa='string', default=C.DEFAULT_PRIVATE_KEY_FILE) |
| 88 |
pipelining = FieldAttribute(isa='bool', default=C.ANSIBLE_PIPELINING) |
| 89 |
|
| 90 |
# networking modules |
| 91 |
network_os = FieldAttribute(isa='string') |
| 92 |
|
| 93 |
# FIXME: docker - remove these |
| 94 |
docker_extra_args = FieldAttribute(isa='string') |
| 95 |
|
| 96 |
# ??? |
| 97 |
connection_lockfd = FieldAttribute(isa='int') |
| 98 |
|
| 99 |
# privilege escalation fields |
| 100 |
become = FieldAttribute(isa='bool') |
| 101 |
become_method = FieldAttribute(isa='string') |
| 102 |
become_user = FieldAttribute(isa='string') |
| 103 |
become_pass = FieldAttribute(isa='string') |
| 104 |
become_exe = FieldAttribute(isa='string', default=C.DEFAULT_BECOME_EXE) |
| 105 |
become_flags = FieldAttribute(isa='string', default=C.DEFAULT_BECOME_FLAGS) |
| 106 |
prompt = FieldAttribute(isa='string') |
| 107 |
|
| 108 |
start_at_task = FieldAttribute(isa='string') |
| 109 |
step = FieldAttribute(isa='bool', default=False) |
| 110 |
|
| 111 |
# "PlayContext.force_handlers should not be used, the calling code should be using play itself instead" |
| 112 |
force_handlers = FieldAttribute(isa='bool', default=False) |
| 113 |
|
| 114 |
def __init__(self, play=None, passwords=None, connection_lockfd=None): |
| 115 |
# Note: play is really not optional. The only time it could be omitted is when we create |
| 116 |
# a PlayContext just so we can invoke its deserialize method to load it from a serialized |
| 117 |
# data source. |
| 118 |
|
| 119 |
super(PlayContext, self).__init__() |
| 120 |
|
| 121 |
if passwords is None: |
| 122 |
passwords = {} |
| 123 |
|
| 124 |
self.password = passwords.get('conn_pass', '') |
| 125 |
self.become_pass = passwords.get('become_pass', '') |
| 126 |
|
| 127 |
self._become_plugin = None |
| 128 |
|
| 129 |
self.prompt = '' |
| 130 |
self.success_key = '' |
| 131 |
|
| 132 |
# a file descriptor to be used during locking operations |
| 133 |
self.connection_lockfd = connection_lockfd |
| 134 |
|
| 135 |
# set options before play to allow play to override them |
| 136 |
if context.CLIARGS: |
| 137 |
self.set_attributes_from_cli() |
| 138 |
else: |
| 139 |
self._internal_verbosity = 0 |
| 140 |
|
| 141 |
if play: |
| 142 |
self.set_attributes_from_play(play) |
| 143 |
|
| 144 |
def set_attributes_from_plugin(self, plugin): |
| 145 |
# generic derived from connection plugin, temporary for backwards compat, in the end we should not set play_context properties |
| 146 |
|
| 147 |
# get options for plugins |
| 148 |
options = C.config.get_configuration_definitions(plugin.plugin_type, plugin._load_name) |
| 149 |
for option in options: |
| 150 |
if option: |
| 151 |
flag = options[option].get('name') |
| 152 |
if flag: |
| 153 |
setattr(self, flag, plugin.get_option(flag)) |
| 154 |
|
| 155 |
def set_attributes_from_play(self, play): |
| 156 |
self.force_handlers = play.force_handlers |
| 157 |
|
| 158 |
def set_attributes_from_cli(self): |
| 159 |
""" |
| 160 |
Configures this connection information instance with data from |
| 161 |
options specified by the user on the command line. These have a |
| 162 |
lower precedence than those set on the play or host. |
| 163 |
""" |
| 164 |
if context.CLIARGS.get('timeout', False): |
| 165 |
self.timeout = int(context.CLIARGS['timeout']) |
| 166 |
|
| 167 |
# From the command line. These should probably be used directly by plugins instead |
| 168 |
# For now, they are likely to be moved to FieldAttribute defaults |
| 169 |
self.private_key_file = context.CLIARGS.get('private_key_file') # Else default |
| 170 |
self._internal_verbosity = context.CLIARGS.get('verbosity') # Else default |
| 171 |
|
| 172 |
# Not every cli that uses PlayContext has these command line args so have a default |
| 173 |
self.start_at_task = context.CLIARGS.get('start_at_task', None) # Else default |
| 174 |
|
| 175 |
def set_task_and_variable_override(self, task, variables, templar): |
| 176 |
""" |
| 177 |
Sets attributes from the task if they are set, which will override |
| 178 |
those from the play. |
| 179 |
|
| 180 |
:arg task: the task object with the parameters that were set on it |
| 181 |
:arg variables: variables from inventory |
| 182 |
:arg templar: templar instance if templating variables is needed |
| 183 |
""" |
| 184 |
|
| 185 |
new_info = self.copy() |
| 186 |
|
| 187 |
# loop through a subset of attributes on the task object and set |
| 188 |
# connection fields based on their values |
| 189 |
for attr in TASK_ATTRIBUTE_OVERRIDES: |
| 190 |
if (attr_val := getattr(task, attr, None)) is not None: |
| 191 |
setattr(new_info, attr, attr_val) |
| 192 |
|
| 193 |
# next, use the MAGIC_VARIABLE_MAPPING dictionary to update this |
| 194 |
# connection info object with 'magic' variables from the variable list. |
| 195 |
# If the value 'ansible_delegated_vars' is in the variables, it means |
| 196 |
# we have a delegated-to host, so we check there first before looking |
| 197 |
# at the variables in general |
| 198 |
if task.delegate_to is not None: |
| 199 |
# In the case of a loop, the delegated_to host may have been |
| 200 |
# templated based on the loop variable, so we try and locate |
| 201 |
# the host name in the delegated variable dictionary here |
| 202 |
delegated_vars = variables.get('ansible_delegated_vars', dict()).get(task.delegate_to, dict()) |
| 203 |
|
| 204 |
delegated_transport = C.DEFAULT_TRANSPORT |
| 205 |
for transport_var in C.MAGIC_VARIABLE_MAPPING.get('connection'): |
| 206 |
if transport_var in delegated_vars: |
| 207 |
delegated_transport = delegated_vars[transport_var] |
| 208 |
break |
| 209 |
|
| 210 |
# make sure this delegated_to host has something set for its remote |
| 211 |
# address, otherwise we default to connecting to it by name. This |
| 212 |
# may happen when users put an IP entry into their inventory, or if |
| 213 |
# they rely on DNS for a non-inventory hostname |
| 214 |
for address_var in ('ansible_%s_host' % delegated_transport,) + C.MAGIC_VARIABLE_MAPPING.get('remote_addr'): |
| 215 |
if address_var in delegated_vars: |
| 216 |
break |
| 217 |
else: |
| 218 |
display.debug("no remote address found for delegated host %s\nusing its name, so success depends on DNS resolution" % task.delegate_to) |
| 219 |
delegated_vars['ansible_host'] = task.delegate_to |
| 220 |
|
| 221 |
# reset the port back to the default if none was specified, to prevent |
| 222 |
# the delegated host from inheriting the original host's setting |
| 223 |
for port_var in ('ansible_%s_port' % delegated_transport,) + C.MAGIC_VARIABLE_MAPPING.get('port'): |
| 224 |
if port_var in delegated_vars: |
| 225 |
break |
| 226 |
else: |
| 227 |
if delegated_transport == 'winrm': |
| 228 |
delegated_vars['ansible_port'] = 5986 |
| 229 |
else: |
| 230 |
delegated_vars['ansible_port'] = C.DEFAULT_REMOTE_PORT |
| 231 |
|
| 232 |
# and likewise for the remote user |
| 233 |
for user_var in ('ansible_%s_user' % delegated_transport,) + C.MAGIC_VARIABLE_MAPPING.get('remote_user'): |
| 234 |
if user_var in delegated_vars and delegated_vars[user_var]: |
| 235 |
break |
| 236 |
else: |
| 237 |
delegated_vars['ansible_user'] = task.remote_user or self.remote_user |
| 238 |
else: |
| 239 |
delegated_vars = dict() |
| 240 |
|
| 241 |
# setup shell |
| 242 |
for exe_var in C.MAGIC_VARIABLE_MAPPING.get('executable'): |
| 243 |
if exe_var in variables: |
| 244 |
setattr(new_info, 'executable', variables.get(exe_var)) |
| 245 |
|
| 246 |
attrs_considered = [] |
| 247 |
for (attr, variable_names) in C.MAGIC_VARIABLE_MAPPING.items(): |
| 248 |
for variable_name in variable_names: |
| 249 |
if attr in attrs_considered: |
| 250 |
continue |
| 251 |
# if delegation task ONLY use delegated host vars, avoid delegated FOR host vars |
| 252 |
if task.delegate_to is not None: |
| 253 |
if isinstance(delegated_vars, dict) and variable_name in delegated_vars: |
| 254 |
setattr(new_info, attr, delegated_vars[variable_name]) |
| 255 |
attrs_considered.append(attr) |
| 256 |
elif variable_name in variables: |
| 257 |
setattr(new_info, attr, variables[variable_name]) |
| 258 |
attrs_considered.append(attr) |
| 259 |
# no else, as no other vars should be considered |
| 260 |
|
| 261 |
# become legacy updates -- from inventory file (inventory overrides |
| 262 |
# commandline) |
| 263 |
for become_pass_name in C.MAGIC_VARIABLE_MAPPING.get('become_pass'): |
| 264 |
if become_pass_name in variables: |
| 265 |
break |
| 266 |
|
| 267 |
# make sure we get port defaults if needed |
| 268 |
if new_info.port is None and C.DEFAULT_REMOTE_PORT is not None: |
| 269 |
new_info.port = int(C.DEFAULT_REMOTE_PORT) |
| 270 |
|
| 271 |
# special overrides for the connection setting |
| 272 |
if len(delegated_vars) > 0: |
| 273 |
# in the event that we were using local before make sure to reset the |
| 274 |
# connection type to the default transport for the delegated-to host, |
| 275 |
# if not otherwise specified |
| 276 |
for connection_type in C.MAGIC_VARIABLE_MAPPING.get('connection'): |
| 277 |
if connection_type in delegated_vars: |
| 278 |
break |
| 279 |
else: |
| 280 |
remote_addr_local = new_info.remote_addr in C.LOCALHOST |
| 281 |
inv_hostname_local = delegated_vars.get('inventory_hostname') in C.LOCALHOST |
| 282 |
if remote_addr_local and inv_hostname_local: |
| 283 |
setattr(new_info, 'connection', 'local') |
| 284 |
elif getattr(new_info, 'connection', None) == 'local' and (not remote_addr_local or not inv_hostname_local): |
| 285 |
setattr(new_info, 'connection', C.DEFAULT_TRANSPORT) |
| 286 |
|
| 287 |
# we store original in 'connection_user' for use of network/other modules that fallback to it as login user |
| 288 |
# connection_user to be deprecated once connection=local is removed for, as local resets remote_user |
| 289 |
if new_info.connection == 'local': |
| 290 |
if not new_info.connection_user: |
| 291 |
new_info.connection_user = new_info.remote_user |
| 292 |
|
| 293 |
# for case in which connection plugin still uses pc.remote_addr and in it's own options |
| 294 |
# specifies 'default: inventory_hostname', but never added to vars: |
| 295 |
if new_info.remote_addr == 'inventory_hostname': |
| 296 |
new_info.remote_addr = variables.get('inventory_hostname') |
| 297 |
display.warning('The "%s" connection plugin has an improperly configured remote target value, ' |
| 298 |
'forcing "inventory_hostname" templated value instead of the string' % new_info.connection) |
| 299 |
|
| 300 |
if task.check_mode is not None: |
| 301 |
new_info.check_mode = task.check_mode |
| 302 |
|
| 303 |
if task.diff is not None: |
| 304 |
new_info.diff = task.diff |
| 305 |
|
| 306 |
return new_info |
| 307 |
|
| 308 |
def set_become_plugin(self, plugin): |
| 309 |
self._become_plugin = plugin |
| 310 |
|
| 311 |
def update_vars(self, variables): |
| 312 |
""" |
| 313 |
Adds 'magic' variables relating to connections to the variable dictionary provided. |
| 314 |
In case users need to access from the play, this is a legacy from runner. |
| 315 |
""" |
| 316 |
|
| 317 |
for prop, var_list in C.MAGIC_VARIABLE_MAPPING.items(): |
| 318 |
try: |
| 319 |
if 'become' in prop: |
| 320 |
continue |
| 321 |
|
| 322 |
var_val = getattr(self, prop) |
| 323 |
for var_opt in var_list: |
| 324 |
if var_opt not in variables and var_val is not None: |
| 325 |
variables[var_opt] = var_val |
| 326 |
except AttributeError: |
| 327 |
continue |
| 328 |
|
| 329 |
def deserialize(self, data): |
| 330 |
"""Do not use this method. Backward compatibility for network connections plugins that rely on it.""" |
| 331 |
self.from_attrs(data) |