Skip to content

Instantly share code, notes, and snippets.

@KeyboardInterrupt
Last active July 15, 2021 06:17
Show Gist options
  • Save KeyboardInterrupt/a885efe64824a070670a6f71db2038ab to your computer and use it in GitHub Desktop.
Save KeyboardInterrupt/a885efe64824a070670a6f71db2038ab to your computer and use it in GitHub Desktop.
I made these Snippets for Inventory to SSH configuration conversion. USE WITH CAUTION the default `StrictHostKeyChecking no` is weakening security by ignoring Host ssh key changes!
#!/bin/bash
cat ~/.ssh/config.d/*
echo "#################################################"
echo "# This will be your new ssh config, is this ok? #"
echo "#################################################"
read -p "Build Config! (y/n)?" choice
case "$choice" in
y|Y ) echo "yes" && cat ~/.ssh/config.d/* > ~/.ssh/config ;;
n|N ) echo "no";;
* ) echo "invalid";;
esac
---
- hosts: localhost
gather_facts: False
vars:
ssh_config_path: ~/.ssh/config.d/
vars_prompt:
- name: "config_name"
prompt: "Name your config file!"
default: "tmp"
private: False
tasks:
- name: generate SSH Config for [all] Hosts
local_action:
module: copy
content: |
## {{ config_name }} ##
{% for host in groups['all'] %}
Host {{ hostvars[host].inventory_hostname }}
StrictHostKeyChecking no
User {{ ansible_user | default(ansible_ssh_user) | default(lookup('env','USER')) }}
ServerAliveInterval 60
ServerAliveCountMax 2
HostName {{ hostvars[host]['ansible_host'] | default( hostvars[host]['ansible_ssh_host'] ) | default("NO_ADDRESS_GIVEN") }}
{% if hostvars[host]['ansible_ssh_common_args'] is defined %}{{ hostvars[host]['ansible_ssh_common_args'].strip('"') | regex_replace('^-o ','') | regex_replace('="',' ') }}{% endif %}
{% endfor %}
dest: "{{ ssh_config_path }}{{ config_name }}"
run_once: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment