Skip to content

Commit ec05e54

Browse files
philfrymoreati
andcommitted
Fix 'ansible_host_key_checking' and 'ansible_ssh_host_key_checking' for
adding new hosts to the inventory using 'add_hosts' Co-authored-by: Alex Willmer <[email protected]>
1 parent 0f34e25 commit ec05e54

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

ansible_mitogen/connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _connect_ssh(spec):
119119
"""
120120
Return ContextService arguments for an SSH connection.
121121
"""
122-
if C.HOST_KEY_CHECKING:
122+
if spec.host_key_checking():
123123
check_host_keys = 'enforce'
124124
else:
125125
check_host_keys = 'ignore'

ansible_mitogen/transport_config.py

+23
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import ansible.constants as C
6868

6969
from ansible.module_utils.six import with_metaclass
70+
from ansible.module_utils.parsing.convert_bool import boolean
7071

7172
# this was added in Ansible >= 2.8.0; fallback to the default interpreter if necessary
7273
try:
@@ -245,6 +246,12 @@ def python_path(self):
245246
Path to the Python interpreter on the target machine.
246247
"""
247248

249+
@abc.abstractmethod
250+
def host_key_checking(self):
251+
"""
252+
Whether or not to check the keys of the target machine
253+
"""
254+
248255
@abc.abstractmethod
249256
def private_key_file(self):
250257
"""
@@ -466,6 +473,14 @@ def python_path(self, rediscover_python=False):
466473
action=self._action,
467474
rediscover_python=rediscover_python)
468475

476+
def host_key_checking(self):
477+
def candidates():
478+
yield self._connection.get_task_var('ansible_ssh_host_key_checking')
479+
yield self._connection.get_task_var('ansible_host_key_checking')
480+
yield C.HOST_KEY_CHECKING
481+
val = next((v for v in candidates() if v is not None), True)
482+
return boolean(val)
483+
469484
def private_key_file(self):
470485
return self._play_context.private_key_file
471486

@@ -692,6 +707,14 @@ def python_path(self, rediscover_python=False):
692707
action=self._action,
693708
rediscover_python=rediscover_python)
694709

710+
def host_key_checking(self):
711+
def candidates():
712+
yield self._host_vars.get('ansible_ssh_host_key_checking')
713+
yield self._host_vars.get('ansible_host_key_checking')
714+
yield C.HOST_KEY_CHECKING
715+
val = next((v for v in candidates() if v is not None), True)
716+
return boolean(val)
717+
695718
def private_key_file(self):
696719
# TODO: must come from PlayContext too.
697720
return (

docs/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Unreleased
2424
* :gh:issue:`952` Fix Ansible `--ask-become-pass`, add test coverage
2525
* :gh:issue:`957` Fix Ansible exception when executing against 10s of hosts
2626
"ValueError: filedescriptor out of range in select()"
27+
* :gh:issue:`1066` Support Ansible `ansible_host_key_checking` & `ansible_ssh_host_key_checking`
2728

2829

2930
v0.3.7 (2024-04-08)

0 commit comments

Comments
 (0)