Skip to content

Commit c1f1486

Browse files
committed
terraform: Use the alternate ssh port for Ansible control
When provisioning terraform instances, make use of the Ansible ssh port setting, in case it is set to something other than port 22. Generated-by: Claude AI Signed-off-by: Chuck Lever <[email protected]>
1 parent f12a190 commit c1f1486

File tree

16 files changed

+162
-16
lines changed

16 files changed

+162
-16
lines changed

playbooks/roles/gen_tfvars/templates/aws/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ aws_ebs_volume_throughput = {{ terraform_aws_ebs_volume_throughput }}
1818
ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
1919
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
2020
ssh_config = "{{ sshconfig }}"
21+
ssh_config_port = {{ ansible_cfg_ssh_port }}
2122

2223
ssh_config_update = "{{ kdevops_terraform_ssh_config_update | lower }}"
2324
ssh_config_use_strict_settings = "{{ kdevops_terraform_ssh_config_update_strict | lower }}"

playbooks/roles/gen_tfvars/templates/azure/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ azure_managed_disks_tier = "{{ terraform_azure_managed_disks_tier }}"
1313
ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
1414
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
1515
ssh_config = "{{ sshconfig }}"
16+
ssh_config_port = {{ ansible_cfg_ssh_port }}
1617

1718
ssh_config_update = "{{ kdevops_terraform_ssh_config_update | lower }}"
1819
ssh_config_use_strict_settings = "{{ kdevops_terraform_ssh_config_update_strict | lower }}"

playbooks/roles/gen_tfvars/templates/gce/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ gce_disk_throughput = {{ terraform_gce_disk_throughput }}
2020
ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
2121
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
2222
ssh_config = "{{ sshconfig }}"
23+
ssh_config_port = {{ ansible_cfg_ssh_port }}
2324

2425
ssh_config_update = "{{ kdevops_terraform_ssh_config_update | lower }}"
2526
ssh_config_use_strict_settings = "{{ kdevops_terraform_ssh_config_update_strict | lower }}"

playbooks/roles/gen_tfvars/templates/lambdalabs/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
77
ssh_config_privkey_file = "{{ kdevops_terraform_ssh_config_privkey_file }}"
88
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
99
ssh_config = "{{ sshconfig }}"
10+
ssh_config_port = {{ ansible_cfg_ssh_port }}
1011
# Use unique SSH config file per directory to avoid conflicts
1112
ssh_config_name = "{{ kdevops_ssh_config_prefix }}{{ topdir_path_sha256sum[:8] }}"
1213

playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ oci_sparse_volume_device_file_name = "{{ terraform_oci_sparse_volume_device_file
2525
ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
2626
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
2727
ssh_config = "{{ sshconfig }}"
28+
ssh_config_port = {{ ansible_cfg_ssh_port }}
2829

2930
ssh_config_update = "{{ kdevops_terraform_ssh_config_update | lower }}"
3031
ssh_config_use_strict_settings = "{{ kdevops_terraform_ssh_config_update_strict | lower }}"

playbooks/roles/gen_tfvars/templates/openstack/terraform.tfvars.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ssh_pubkey_name = "{{ terraform_openstack_ssh_pubkey_name }}"
77
ssh_config_pubkey_file = "{{ kdevops_terraform_ssh_config_pubkey_file }}"
88
ssh_config_user = "{{ kdevops_terraform_ssh_config_user }}"
99
ssh_config = "{{ sshconfig }}"
10+
ssh_config_port = {{ ansible_cfg_ssh_port }}
1011

1112
ssh_config_update = "{{ kdevops_terraform_ssh_config_update | lower }}"
1213
ssh_config_use_strict_settings = "{{ kdevops_terraform_ssh_config_update_strict | lower }}"

playbooks/roles/terraform/templates/ssh_config.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Host {{ item.key }} {{ item.value }}
22
HostName {{ item.value }}
33
User {{ kdevops_terraform_ssh_config_user }}
4-
Port 22
4+
Port {{ ansible_cfg_ssh_port }}
55
IdentityFile {{ kdevops_terraform_ssh_config_privkey_file }}
66
{% if ssh_config_kexalgorithms %}
77
KexAlgorithms {{ ssh_config_kexalgorithms }}

scripts/update_ssh_config_lambdalabs.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def update_ssh_config(
14-
action, hostname, ip_address, username, config_file, ssh_key, provider_name
14+
action, hostname, ip_address, username, config_file, ssh_key, provider_name, port=22
1515
):
1616
"""
1717
Update SSH configuration file with Lambda Labs instance details.
@@ -24,6 +24,7 @@ def update_ssh_config(
2424
config_file: SSH config file path
2525
ssh_key: Path to SSH private key
2626
provider_name: Provider name for comments
27+
port: SSH port number (default: 22)
2728
"""
2829
config_file = os.path.expanduser(config_file)
2930
ssh_key = os.path.expanduser(ssh_key)
@@ -33,7 +34,7 @@ def update_ssh_config(
3334
Host {hostname} {ip_address}
3435
\tHostName {ip_address}
3536
\tUser {username}
36-
\tPort 22
37+
\tPort {port}
3738
\tIdentityFile {ssh_key}
3839
\tUserKnownHostsFile /dev/null
3940
\tStrictHostKeyChecking no
@@ -90,7 +91,7 @@ def main():
9091
"""Main entry point."""
9192
if len(sys.argv) < 7:
9293
print(
93-
f"Usage: {sys.argv[0]} <action> <hostname> <ip_address> <username> <config_file> <ssh_key> [provider_name]"
94+
f"Usage: {sys.argv[0]} <action> <hostname> <ip_address> <username> <config_file> <ssh_key> [provider_name] [port]"
9495
)
9596
print(" action: 'update' or 'remove'")
9697
print(" hostname: Instance hostname")
@@ -99,6 +100,7 @@ def main():
99100
print(" config_file: SSH config file path")
100101
print(" ssh_key: Path to SSH private key")
101102
print(" provider_name: Optional provider name (default: 'Lambda Labs')")
103+
print(" port: Optional SSH port (default: 22)")
102104
sys.exit(1)
103105

104106
action = sys.argv[1]
@@ -108,9 +110,17 @@ def main():
108110
config_file = sys.argv[5]
109111
ssh_key = sys.argv[6]
110112
provider_name = sys.argv[7] if len(sys.argv) > 7 else "Lambda Labs"
113+
port = int(sys.argv[8]) if len(sys.argv) > 8 else 22
111114

112115
update_ssh_config(
113-
action, hostname, ip_address, username, config_file, ssh_key, provider_name
116+
action,
117+
hostname,
118+
ip_address,
119+
username,
120+
config_file,
121+
ssh_key,
122+
provider_name,
123+
port,
114124
)
115125

116126

terraform/aws/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ resource "aws_security_group" "kdevops_sec_group" {
3939
cidr_blocks = [
4040
"0.0.0.0/0",
4141
]
42-
from_port = 22
43-
to_port = 22
42+
from_port = var.ssh_config_port
43+
to_port = var.ssh_config_port
4444
protocol = "tcp"
4545
}
4646

@@ -82,6 +82,7 @@ data "template_file" "script_user_data" {
8282
user_data_log_dir = var.user_data_log_dir
8383
user_data_enabled = var.user_data_enabled
8484
ssh_config_user = var.ssh_config_user
85+
ssh_config_port = var.ssh_config_port
8586
new_hostname = element(var.kdevops_nodes, count.index),
8687
}
8788
}

terraform/azure/main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ resource "azurerm_network_security_group" "kdevops_sg" {
4343
access = "Allow"
4444
protocol = "Tcp"
4545
source_port_range = "*"
46-
destination_port_range = "22"
46+
destination_port_range = tostring(var.ssh_config_port)
4747
source_address_prefix = "*"
4848
destination_address_prefix = "*"
4949
}
@@ -89,6 +89,13 @@ resource "azurerm_linux_virtual_machine" "kdevops_vm" {
8989
size = var.azure_vmsize
9090
admin_username = var.ssh_config_user
9191
disable_password_authentication = true
92+
custom_data = base64encode(templatefile("${path.module}/../scripts/cloud-init.sh", {
93+
user_data_log_dir = "/var/log/kdevops"
94+
user_data_enabled = "yes"
95+
ssh_config_user = var.ssh_config_user
96+
ssh_config_port = var.ssh_config_port
97+
new_hostname = element(var.kdevops_nodes, count.index)
98+
}))
9299

93100
os_disk {
94101
# Note: yes using the names like the ones below is better however it also

0 commit comments

Comments
 (0)