Skip to content

Commit 41cda98

Browse files
committed
fix: [#222] Make SSH port configuration conditional on non-default port
The cloud-init template was unconditionally configuring SSH port and rebooting the VM, even when using the default port 22. This caused: - Unnecessary VM reboots for environments using default SSH port - E2E infrastructure lifecycle test failures on GitHub Actions - Longer provisioning times for default configurations Root Cause: - Cloud-init template always wrote SSH port config file and triggered reboot - E2E test uses default port 22, but was forced to reboot unnecessarily - GitHub runner timed out waiting for SSH after unnecessary reboot Solution: - Add Tera conditional {% if ssh_port != 22 %} around write_files and runcmd - SSH port configuration and reboot now only happen for custom ports - E2E tests with default port 22 no longer trigger unnecessary reboots - Provisioning is faster for default port configurations Benefits: - Faster provisioning when using default SSH port (no reboot overhead) - E2E tests pass on GitHub Actions without timeout issues - Still maintains reboot pattern for custom ports (proper SSH restart) - Conditional approach is more efficient and user-friendly Files Modified: - templates/tofu/common/cloud-init.yml.tera: Add conditional around write_files and runcmd - docs/issues/222-configure-ssh-service-port.md: Document conditional behavior - docs/decisions/cloud-init-ssh-port-reboot.md: Add positive consequence about conditional execution Technical Details: The Tera template now checks if ssh_port != 22 before: 1. Writing /etc/ssh/sshd_config.d/99-custom-port.conf 2. Executing runcmd: [reboot] This preserves the reboot pattern for custom ports (ensuring clean SSH restart) while avoiding unnecessary reboots for default port configurations. Testing: - All 1424 unit tests pass - All doctests pass - E2E infrastructure lifecycle tests pass (default port 22, no reboot) - E2E deployment workflow tests pass - Documentation builds successfully - Pre-commit verification: ✅ All checks passed
1 parent 06bb95f commit 41cda98

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

docs/decisions/cloud-init-ssh-port-reboot.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ We configure the custom SSH port via cloud-init using the **`write_files` + `reb
3131
1. **Write SSH configuration file** using cloud-init's `write_files` directive:
3232

3333
```yaml
34+
{% if ssh_port != 22 %}
3435
write_files:
3536
- path: /etc/ssh/sshd_config.d/99-custom-port.conf
3637
content: |
@@ -45,8 +46,11 @@ We configure the custom SSH port via cloud-init using the **`write_files` + `reb
4546
```yaml
4647
runcmd:
4748
- reboot
49+
{% endif %}
4850
```
4951

52+
**Conditional Configuration**: The SSH port configuration and reboot only execute when `ssh_port != 22`, avoiding unnecessary reboots for environments using the default SSH port.
53+
5054
The reboot ensures:
5155

5256
- SSH service cleanly restarts with the new configuration
@@ -70,6 +74,7 @@ Additionally, we made two critical fixes to the provision handler:
7074
- **Correct architecture**: Infrastructure configuration happens during infrastructure provisioning
7175
- **No special cases**: Ansible can connect normally using the configured port without overrides or workarounds
7276
- **Compile-time safety**: Provision handler correctly waits for the configured port, preventing connection failures
77+
- **Conditional execution**: Only reboots when custom port is needed (ssh_port != 22), avoiding unnecessary reboots for default configurations
7378

7479
### Negative
7580

docs/issues/222-configure-ssh-service-port.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The SSH port is configured by:
6666
The cloud-init template uses the **reboot pattern** as documented in [Hetzner's cloud-config tutorial](https://community.hetzner.com/tutorials/basic-cloud-config):
6767

6868
```yaml
69+
{% if ssh_port != 22 %}
6970
write_files:
7071
- path: /etc/ssh/sshd_config.d/99-custom-port.conf
7172
content: |
@@ -79,8 +80,11 @@ runcmd:
7980
# The reboot ensures SSH service fully restarts with the new port from write_files
8081
# This is the recommended approach per Hetzner cloud-config best practices
8182
- reboot
83+
{% endif %}
8284
```
8385

86+
**Important**: The SSH port configuration and reboot are **conditional** - they only execute when `ssh_port != 22`. This avoids unnecessary reboots for environments using the default SSH port.
87+
8488
**Why reboot?** Three critical reasons (from Hetzner documentation):
8589

8690
1. Package updates may require reboot for patches to work properly

templates/tofu/common/cloud-init.yml.tera

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ users:
3232
# SSH public key injected from SshConfig.ssh_pub_key_path
3333
- {{ ssh_public_key }}
3434

35+
{% if ssh_port != 22 %}
3536
write_files:
3637
- path: /etc/ssh/sshd_config.d/99-custom-port.conf
3738
content: |
@@ -45,4 +46,5 @@ runcmd:
4546
# The reboot ensures SSH service fully restarts with the new port from write_files
4647
# This is the recommended approach per Hetzner cloud-config best practices
4748
- reboot
49+
{% endif %}
4850

0 commit comments

Comments
 (0)