Fix passing callbacks the delegated connection, host, port, and user (#85397)

Fix ssh plugin host variables. Variable names should conform to Python variable naming rules.
, and not contain characters like "[" or "]".

Update unit test
pull/85462/head
Sloane Hertel 5 months ago committed by GitHub
parent c24b187f88
commit da6735160d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
bugfixes:
- callback plugins - fix displaying the rendered ``ansible_host`` variable with ``delegate_to`` (https://github.com/ansible/ansible/issues/84922).
- ssh connection - fix documented variables for the ``host`` option. Connection options can be configured with delegated variables in general.

@ -19,7 +19,7 @@ from ansible.errors import (
AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleTaskError,
AnsibleValueOmittedError,
)
from ansible.executor.task_result import _RawTaskResult
from ansible.executor.task_result import _RawTaskResult, _SUB_PRESERVE
from ansible._internal._datatag import _utils
from ansible.module_utils._internal import _messages
from ansible.module_utils.datatag import native_type_name, deprecator_from_collection_name
@ -771,14 +771,18 @@ class TaskExecutor:
# on the results side without having to do any further templating
# also now add connection vars results when delegating
if self._task.delegate_to:
result["_ansible_delegated_vars"] = {'ansible_delegated_host': self._task.delegate_to}
for k in plugin_vars:
result["_ansible_delegated_vars"][k] = cvars.get(k)
result["_ansible_delegated_vars"] = {
"ansible_delegated_host": self._task.delegate_to,
"ansible_connection": current_connection,
}
# note: here for callbacks that rely on this info to display delegation
for requireshed in ('ansible_host', 'ansible_port', 'ansible_user', 'ansible_connection'):
if requireshed not in result["_ansible_delegated_vars"] and requireshed in cvars:
result["_ansible_delegated_vars"][requireshed] = cvars.get(requireshed)
for k in plugin_vars:
if k not in _SUB_PRESERVE["_ansible_delegated_vars"]:
continue
for o in C.config.get_plugin_options_from_var("connection", current_connection, k):
result["_ansible_delegated_vars"][k] = self._connection.get_option(o)
# and return
display.debug("attempt loop complete, returning result")

@ -34,8 +34,6 @@ DOCUMENTATION = """
- name: inventory_hostname
- name: ansible_host
- name: ansible_ssh_host
- name: delegated_vars['ansible_host']
- name: delegated_vars['ansible_ssh_host']
host_key_checking:
description: Determines if SSH should reject or not a connection after checking host keys.
default: True

@ -1,11 +1,3 @@
- name: setup delegated host
hosts: localhost
gather_facts: false
tasks:
- add_host:
name: delegatetome
ansible_host: 127.0.0.4
- name: ensure we dont use orig host vars if delegated one does not define them
hosts: testhost
gather_facts: false

@ -0,0 +1,2 @@
[all]
delegatetome ansible_host="{{ hostip }}" hostip="127.0.0.4"

@ -57,7 +57,11 @@ ansible-playbook delegate_facts_block.yml -i inventory -v "$@"
ansible-playbook test_delegate_to_loop_caching.yml -i inventory -v "$@"
# ensure we are using correct settings when delegating
ANSIBLE_TIMEOUT=3 ansible-playbook delegate_vars_handling.yml -i inventory -v "$@"
ANSIBLE_TIMEOUT=3 ansible-playbook delegate_vars_handling.yml -i inventory -i delegate_vars_inventory -v | tee out
if grep '{{ hostip }}' out; then
echo 'Callback displayed the ansible_host template instead of the rendered value.'
exit 1
fi
ansible-playbook has_hostvars.yml -i inventory -v "$@"

@ -210,8 +210,8 @@ class TestConnectionBaseClass(unittest.TestCase):
mock_ospe.return_value = True
conn._build_command.return_value = 'some command to run'
conn._bare_run.return_value = (0, '', '')
conn.host = "some_host"
conn.set_option("host", "some_host")
conn.set_option('reconnection_retries', 9)
conn.set_option('ssh_transfer_method', None) # default is smart
@ -261,8 +261,8 @@ class TestConnectionBaseClass(unittest.TestCase):
conn._build_command.return_value = 'some command to run'
conn._bare_run.return_value = (0, '', '')
conn.host = "some_host"
conn.set_option("host", "some_host")
conn.set_option('reconnection_retries', 9)
conn.set_option('ssh_transfer_method', None) # default is smart

Loading…
Cancel
Save