From 2511faa114a061a3029db0575bb69edaa66c9626 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 3 Oct 2020 21:40:34 +0200 Subject: move known-host cleanup to utils.sh --- common/utils.sh | 28 ++++++++++++++++++++++++++++ install.sh | 2 +- remove-known-host.sh | 20 +------------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/common/utils.sh b/common/utils.sh index d8e459a1..5199c3b6 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -37,6 +37,34 @@ ansible_variable__get() { } +########################### +## remove ssh known_hosts entries + +remove_known_hosts() { + inventory_hostname="$1" + ssh_hostname=$(ssh -G "$inventory_hostname" | grep "^hostname " | awk '{ print($2) }' ) + ssh_port=$(ssh -G "$inventory_hostname" | grep "^port " | awk '{ print($2) }' ) + known_hosts_file=$(ssh -G "$inventory_hostname" | grep "^userknownhostsfile " | awk '{ print($2) }' ) + known_hosts_file=${known_hosts_file/#\~/$HOME} + + declare -a names + names+=("$inventory_hostname") + names+=("$ssh_hostname") + names+=("$ssh_hostname:$ssh_port") + names+=("[$ssh_hostname]:$ssh_port") + + ansible_variable__get ansible_host "$inventory_hostname" || exit 1 + names+=("$ansible_host") + ansible_variable__get host_name "$inventory_hostname" || exit 1 + names+=("$host_name") + ansible_variable__get host_domain "$inventory_hostname" > /dev/null 2>&1 && names+=("$host_name.$host_domain") + + for name in ${names[@]} ; do + ssh-keygen -f "$known_hosts_file" -R "$name" + done +} + + ########################### ## vault environment handling diff --git a/install.sh b/install.sh index e91ed60b..b4056858 100755 --- a/install.sh +++ b/install.sh @@ -18,7 +18,7 @@ echo "installing $name with $distro/$codename in environment '$env_group'" echo "" echo "########## clearing old ssh host keys #########" -./remove-known-host.sh "$name" +remove_known_hosts "$name" echo "" echo "########## removing cached facts #########" diff --git a/remove-known-host.sh b/remove-known-host.sh index 647909ea..9035935e 100755 --- a/remove-known-host.sh +++ b/remove-known-host.sh @@ -5,26 +5,8 @@ if [ -z "$1" ]; then exit 1 fi -short="$1" -ssh_host=$(ssh -G "$short" | grep "^hostname " | awk '{ print($2) }' ) -ssh_port=$(ssh -G "$short" | grep "^port " | awk '{ print($2) }' ) -known_hosts_file=$(ssh -G "$short" | grep "^userknownhostsfile " | awk '{ print($2) }' ) -known_hosts_file=${known_hosts_file/#\~/$HOME} - -declare -a names -names+=("$short") -names+=("$ssh_host") -names+=("$ssh_host:$ssh_port") -names+=("[$ssh_host]:$ssh_port") - cd "${BASH_SOURCE%/*}" source common/utils.sh -ansible_variable__get host_name "$short" || exit 1 -names+=("$host_name") -ansible_variable__get host_domain "$short" > /dev/null 2>&1 && names+=("$host_name.$host_domain") - -for name in ${names[@]} ; do - ssh-keygen -f "$known_hosts_file" -R "$name" -done +remove_known_hosts "$1" exit 0 -- cgit v1.2.3