diff --git a/ansible/create_ssh_config.j2 b/ansible/create_ssh_config.j2 new file mode 100644 index 0000000..207b421 --- /dev/null +++ b/ansible/create_ssh_config.j2 @@ -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 %} \ No newline at end of file diff --git a/ansible/create_ssh_config.sh b/ansible/create_ssh_config.sh new file mode 100755 index 0000000..f07a76f --- /dev/null +++ b/ansible/create_ssh_config.sh @@ -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 \ No newline at end of file diff --git a/ansible/create_ssh_config.yml b/ansible/create_ssh_config.yml new file mode 100644 index 0000000..1de422d --- /dev/null +++ b/ansible/create_ssh_config.yml @@ -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 \ No newline at end of file