Created
August 17, 2024 03:14
-
-
Save mohshami/ea88c2ba63d13ac928501208fe9da3a5 to your computer and use it in GitHub Desktop.
Templating ssh_config with gomplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jq -s '.' hosts/* | gomplate -f ssh_config.tmpl -d server=stdin:///in.json > ~/.ssh/config.inc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "neo", | |
"ips": ["10.1.0.1"], | |
"key": "ed25519", | |
"comment": "NAS", | |
"env": { | |
"TERM": "xterm-ghostty" | |
}, | |
"proxy": [ | |
["home", "none"] | |
] | |
} | |
{ | |
"name": "jarvis", | |
"ips": ["192.168.0.1"], | |
"user": "mohshami", | |
"key": "ed25519", | |
"proxy": [ | |
["home", "none"] | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddKeysToAgent yes | |
{{ range (ds "server") }} | |
{{ if ne (index . "comment") nil }}# {{ .comment }}{{ end }} | |
{{- $item := . -}} | |
{{ range (index . "proxy" | default (slice (dict "none" "none"))) }} | |
Match host {{ $item.name }},{{ join ($item.ips | default slice) "," }}{{ if ne (index . 0) "none" }} exec "~/.ssh/network_detect/{{ index . 0 }}"{{ end }} | |
{{ if not (contains (index (index $item "ips") 0) "*") }} HostName {{ index (index $item "ips") 0 }}{{ end }} | |
User {{ index $item "user" | default "root" }} | |
{{- if index $item "key" }} | |
IdentityFile ~/.ssh/public_keys/{{ $item.key }}.pub | |
{{- end -}} | |
{{- range $env, $env_val := (index $item "env" | default slice) }} | |
SetEnv {{ $env }}={{ $env_val }} | |
{{- end }} | |
ProxyJump {{ index . 1 }} | |
{{ end -}} | |
{{ end }} | |
# Use the "Host" directive to allow shell completion | |
{{- range (ds "server") }} | |
Host {{ .name }} | |
{{- end }} | |
{{ "" }} | |
Host * | |
ServerAliveInterval 30 | |
ServerAliveCountMax 2 | |
SetEnv TERM=xterm-256color |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment