connection: use inventory_hostname for generating control path

* Make control path unique by using inventory_hostname

Fixes: #76956

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/84135/head
Abhijeet Kasurde 1 month ago
parent c6e166319a
commit 6f42c6064e

@ -0,0 +1,4 @@
---
bugfixes:
- connection - use inventory_hostname while generating control path directory name to make it more unique (https://github.com/ansible/ansible/issues/76956).

@ -993,7 +993,8 @@ class TaskExecutor:
self._play_context, self._play_context,
self._new_stdin, self._new_stdin,
task_uuid=self._task._uuid, task_uuid=self._task._uuid,
ansible_playbook_pid=to_text(os.getppid()) ansible_playbook_pid=to_text(os.getppid()),
inventory_hostname=cvars.get("inventory_hostname")
) )
if not connection: if not connection:

@ -103,6 +103,7 @@ class ConnectionBase(AnsiblePlugin):
shell_type = play_context.shell if play_context.shell else getattr(self, '_shell_type', None) shell_type = play_context.shell if play_context.shell else getattr(self, '_shell_type', None)
self._shell = get_shell_plugin(shell_type=shell_type, executable=self._play_context.executable) self._shell = get_shell_plugin(shell_type=shell_type, executable=self._play_context.executable)
self._inventory_hostname = kwargs.get("inventory_hostname")
self.become: BecomeBase | None = None self.become: BecomeBase | None = None
@property @property

@ -597,13 +597,16 @@ class Connection(ConnectionBase):
user: str | None, user: str | None,
connection: ConnectionBase | None = None, connection: ConnectionBase | None = None,
pid: int | None = None, pid: int | None = None,
inventory_hostname: str | None = None,
) -> str: ) -> str:
"""Make a hash for the controlpath based on con attributes""" """Make a hash for the controlpath based on con attributes"""
pstring = '%s-%s-%s' % (host, port, user) pstring = f"{host}-{port}-{user}"
if connection: if connection:
pstring += '-%s' % connection pstring += f"-{connection}"
if pid: if pid:
pstring += '-%s' % to_text(pid) pstring += f"-{pid}"
if inventory_hostname:
pstring += f"-{inventory_hostname}"
m = hashlib.sha1() m = hashlib.sha1()
m.update(to_bytes(pstring)) m.update(to_bytes(pstring))
digest = m.hexdigest() digest = m.hexdigest()
@ -810,7 +813,8 @@ class Connection(ConnectionBase):
self.control_path = self._create_control_path( self.control_path = self._create_control_path(
self.host, self.host,
self.port, self.port,
self.user self.user,
inventory_hostname=self._inventory_hostname,
) )
b_args = (b"-o", b'ControlPath="%s"' % to_bytes(self.control_path % dict(directory=cpdir), errors='surrogate_or_strict')) b_args = (b"-o", b'ControlPath="%s"' % to_bytes(self.control_path % dict(directory=cpdir), errors='surrogate_or_strict'))
self._add_args(b_command, b_args, u"found only ControlPersist; added ControlPath") self._add_args(b_command, b_args, u"found only ControlPersist; added ControlPath")

@ -0,0 +1,9 @@
[web]
web1
web2
[web:vars]
ansible_host=localhost
ansible_connection=ssh
ansible_python_interpreter="{{ ansible_playbook_python }}"
ansible_ssh_pipelining=true

@ -79,3 +79,9 @@ ANSIBLE_CONFIG=./test_ssh_defaults.cfg ansible-playbook verify_config.yml "$@"
# ensure we handle cp with spaces correctly, otherwise would fail with # ensure we handle cp with spaces correctly, otherwise would fail with
# `"Failed to connect to the host via ssh: command-line line 0: keyword controlpath extra arguments at end of line"` # `"Failed to connect to the host via ssh: command-line line 0: keyword controlpath extra arguments at end of line"`
ANSIBLE_SSH_CONTROL_PATH='/tmp/ssh cp with spaces' ansible -m ping all -e ansible_connection=ssh -i test_connection.inventory "$@" ANSIBLE_SSH_CONTROL_PATH='/tmp/ssh cp with spaces' ansible -m ping all -e ansible_connection=ssh -i test_connection.inventory "$@"
# Check if control path is different
ansible -m ping -i control_path.inventory web -vvv 1> stdout.txt 2> /dev/null
first=$(cat stdout.txt|grep ControlPath= | head -1)
second=$(cat stdout.txt|grep ControlPath= | tail -1)
test "$first" != "$second"

Loading…
Cancel
Save