add create ssh config script

This commit is contained in:
2026-02-03 18:49:15 -06:00
parent 7046191ef1
commit 332f7bf983
3 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
## From https://github.com/russStarr/ssh_config_from_inventory
## License: Apache 2.0 (yes, that is all that was included in the original)
{% for host in groups['all'] %}
{% if keepgroupnames is defined and keepgroupnames == "True" %}
Host {{ hostvars[host]['inventory_hostname_short'] }}{% for group in hostvars[host]['group_names'] %} {{ group }}.{{ hostvars[host]['inventory_hostname_short'] }}{% endfor %}
{% else %}
Host {{ hostvars[host]['inventory_hostname_short'] }}
{% endif %}
{% if hostvars[host]['ansible_host'] is defined %}
HostName {{ hostvars[host]['ansible_host'] }}
{% elif hostvars[host]['ansible_ssh_host'] is defined %}
HostName {{ hostvars[host]['ansible_ssh_host'] }}
{% else %}
HostName {{ host }}
{% endif %}
{% if hostvars[host]['ansible_port'] is defined %}
Port {{ hostvars[host]['ansible_port'] }}
{% elif hostvars[host]['ansible_ssh_port'] is defined %}
Port {{ hostvars[host]['ansible_ssh_port'] }}
{% endif %}
{% if hostvars[host]['ansible_user'] is defined %}
User {{ hostvars[host]['ansible_user'] }}
{% elif hostvars[host]['ansible_ssh_user'] is defined %}
User {{ hostvars[host]['ansible_ssh_user'] }}
{% endif %}
{% if hostvars[host]['ansible_ssh_private_key_file'] is defined %}
IdentityFile {{ hostvars[host]['ansible_ssh_private_key_file'] }}
{% endif %}
{% if hostvars[host]['ansible_ssh_common_args'] is defined %}
ProxyCommand {{ hostvars[host]['ansible_ssh_common_args'] | regex_replace('^.*ProxyCommand=\'(.*?)\'.*', '\\1') }}
{% endif %}
{% endfor %}

11
ansible/create_ssh_config.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Author: Landon Dyck
# This will use an ansible host file to create an ssh config file and ensure it
# is included in your ssh config
# Usage: $ ./create_ssh_config.sh [HOST_FILE=ansible/hosts]
PATH=${PWD}/env/bin:${PATH}
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
HOST_FILE="${HOST_FILE:-ansible/hosts}"
ansible-playbook $SCRIPT_DIR/create_ssh_config.yml -i $HOST_FILE

View File

@@ -0,0 +1,25 @@
- hosts: localhost
connection: local
gather_facts: false
vars:
include_path: ~/.ssh/config.d/codemonkey.config
tasks:
- name: Include ssh config dir
lineinfile:
path: ~/.ssh/config
line: Include ~/.ssh/config.d/*
state: present
insertbefore: BOF
- name: ensure ssh config dir exists
file:
path: ~/.ssh/config.d
state: directory
- name: Creating local SSH config files
template:
lstrip_blocks: true
src: create_ssh_config.j2
dest: "{{ include_path }}"
mode: 0644