add create ssh config script
This commit is contained in:
34
ansible/create_ssh_config.j2
Normal file
34
ansible/create_ssh_config.j2
Normal 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
11
ansible/create_ssh_config.sh
Executable 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
|
||||||
25
ansible/create_ssh_config.yml
Normal file
25
ansible/create_ssh_config.yml
Normal 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
|
||||||
Reference in New Issue
Block a user