Skip to content

Commit 0d09174

Browse files
authored
Merge pull request #1184 from moreati/issue1083-host_key_checking
ansible_mitogen: Templated SSH host key checking
2 parents 9189c01 + 3a1b5ec commit 0d09174

File tree

10 files changed

+58
-24
lines changed

10 files changed

+58
-24
lines changed

.ci/localhost_ansible_tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
subprocess.check_call('sudo chmod 700 ~root/.ssh', shell=True)
5252
subprocess.check_call('sudo chmod 600 ~root/.ssh/authorized_keys', shell=True)
5353

54+
os.chdir(IMAGE_PREP_DIR)
55+
ci_lib.run("ansible-playbook -c local -i localhost, macos_localhost.yml")
56+
5457
if os.path.expanduser('~mitogen__user1') == '~mitogen__user1':
5558
os.chdir(IMAGE_PREP_DIR)
5659
ci_lib.run("ansible-playbook -c local -i localhost, _user_accounts.yml")

ansible_mitogen/transport_config.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,7 @@ def python_path(self, rediscover_python=False):
500500
rediscover_python=rediscover_python)
501501

502502
def host_key_checking(self):
503-
def candidates():
504-
yield self._connection.get_task_var('ansible_ssh_host_key_checking')
505-
yield self._connection.get_task_var('ansible_host_key_checking')
506-
yield C.HOST_KEY_CHECKING
507-
val = next((v for v in candidates() if v is not None), True)
508-
return boolean(val)
503+
return self._connection_option('host_key_checking')
509504

510505
def private_key_file(self):
511506
return self._connection_option('private_key_file')

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ In progress (unreleased)
2525
on SSH key ``mitogen__has_sudo_pubkey.key`` during Ansible tests.
2626
* :gh:issue:`1083` :mod:`ansible_mitogen`: Templated SSH private key file
2727
(e.g. ``ansible_private_key_file``).
28+
* :gh:issue:`1083` :mod:`ansible_mitogen`: Templated SSH host key checking
29+
(e.g. ``ansible_host_key_checking``, ``ansible_ssh_host_key_checking``).
2830

2931

3032
v0.3.16 (2024-11-05)

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ansible_host=localhost
4343
ansible_user="{{ lookup('pipe', 'whoami') }}"
4444

4545
[tt_targets_inventory]
46+
tt-host-key-checking ansible_host_key_checking="{{ 'false' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
4647
tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
4748
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
4849
tt-private-key-file ansible_private_key_file="{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ansible_python_interpreter={{ tt.python_path }}
7171
ansible_user=mitogen__has_sudo_nopw
7272

7373
[tt_targets_inventory]
74+
tt-host-key-checking ansible_host_key_checking="{{ '{{' }} 'false' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
7475
tt-password ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
7576
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
7677
tt-private-key-file ansible_port={{ tt.port }} ansible_private_key_file="{{ '{{' }} git_basedir {{ '}}' }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey

tests/image_prep/_container_setup.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
gather_facts: true
2424
vars:
2525
distro: "{{ansible_distribution}}"
26-
tasks:
27-
- when: ansible_virtualization_type != "docker"
28-
meta: end_play
2926

27+
pre_tasks:
28+
- meta: end_play
29+
when:
30+
- ansible_facts.virtualization_type != "docker"
31+
32+
roles:
33+
- role: sshd
34+
35+
tasks:
3036
- name: Ensure requisite apt packages are installed
3137
apt:
3238
name: "{{ common_packages + packages }}"
@@ -134,10 +140,6 @@
134140
content: |
135141
i-am-mitogen-test-docker-image
136142
137-
- copy:
138-
dest: /etc/ssh/banner.txt
139-
src: ../data/docker/ssh_login_banner.txt
140-
141143
- name: Ensure /etc/sudoers.d exists
142144
file:
143145
state: directory
@@ -169,17 +171,6 @@
169171
line: "%wheel ALL=(ALL) ALL"
170172
when: ansible_os_family == 'RedHat'
171173

172-
- name: Enable SSH banner
173-
lineinfile:
174-
path: /etc/ssh/sshd_config
175-
line: Banner /etc/ssh/banner.txt
176-
177-
- name: Allow remote SSH root login
178-
lineinfile:
179-
path: /etc/ssh/sshd_config
180-
line: PermitRootLogin yes
181-
regexp: '.*PermitRootLogin.*'
182-
183174
- name: Allow remote SSH root login
184175
lineinfile:
185176
path: /etc/pam.d/sshd
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- name: Configure macOS
2+
hosts: all
3+
gather_facts: true
4+
strategy: mitogen_free
5+
become: true
6+
roles:
7+
- role: sshd
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sshd_config_file: /etc/ssh/sshd_config
2+
3+
sshd_config__max_auth_tries: 50
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- name: Create login banner
2+
copy:
3+
src: banner.txt
4+
dest: /etc/ssh/banner.txt
5+
mode: u=rw,go=r
6+
7+
- name: Configure sshd_config
8+
lineinfile:
9+
path: "{{ sshd_config_file }}"
10+
line: "{{ item.line }}"
11+
regexp: "{{ item.regexp }}"
12+
loop:
13+
- line: Banner /etc/ssh/banner.txt
14+
regexp: '^#? *Banner.*'
15+
- line: MaxAuthTries {{ sshd_config__max_auth_tries }}
16+
regexp: '^#? *MaxAuthTries.*'
17+
- line: PermitRootLogin yes
18+
regexp: '.*PermitRootLogin.*'
19+
loop_control:
20+
label: "{{ item.line }}"
21+
register: configure_sshd_result
22+
23+
- name: Restart sshd
24+
shell: |
25+
launchctl unload /System/Library/LaunchDaemons/ssh.plist
26+
wait 5
27+
launchctl load -w /System/Library/LaunchDaemons/ssh.plist
28+
changed_when: true
29+
when:
30+
- ansible_facts.distribution == "MacOSX"
31+
- configure_sshd_result is changed

0 commit comments

Comments
 (0)