vm-install: Remove hosts from known hosts in the playbook
authornicoo <nicoo@realraum.at>
Wed, 13 Jun 2018 22:18:51 +0000 (00:18 +0200)
committernicoo <nicoo@realraum.at>
Sun, 17 Jun 2018 10:06:58 +0000 (12:06 +0200)
This avoid relying on localconfig being used.

ansible/remove-known-host.sh [deleted file]
ansible/remove_known_hosts.sh [new file with mode: 0755]
ansible/remove_known_hosts.yml [new file with mode: 0644]
ansible/vm-install.sh
ansible/vm-install.yml

diff --git a/ansible/remove-known-host.sh b/ansible/remove-known-host.sh
deleted file mode 100755 (executable)
index 2c5fd7f..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-if [ -z "$1" ]; then
-  echo "$0 <host>"
-  exit 1
-fi
-
-SHORT="r3-${1%%.*}"
-SSH_HOST=$(ssh -G "$SHORT" | grep "^hostname " | awk '{ print($2) }' )
-
-for name in $SHORT $SSH_HOST; do
-  ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[$name]:22000"
-done
-
-exit 0
diff --git a/ansible/remove_known_hosts.sh b/ansible/remove_known_hosts.sh
new file mode 100755 (executable)
index 0000000..ee4a0fb
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+set -eu
+
+if [ $# -eq 0 ]; then
+    echo "Usage: $0 vmname [vmname ...]" >&2
+    exit 1
+fi
+
+cd "$(dirname "$0")"
+
+for vmname in "$@"; do
+    ansible-playbook -e vmname="${vmname}" remove_known_hosts.yml
+done
diff --git a/ansible/remove_known_hosts.yml b/ansible/remove_known_hosts.yml
new file mode 100644 (file)
index 0000000..5491342
--- /dev/null
@@ -0,0 +1,11 @@
+- name: Purge host from known hosts
+  hosts: localhost
+  gather_facts: no
+  tasks:
+    - command: ssh-keygen -f ~/.ssh/known_hosts -R [{{ item }}]:{{ hostvars[vmname].ansible_port }}
+      with_items:
+        - "{{ hostvars[vmname].ansible_host }}"
+        - r3-{{ vmname }}
+        - r3g-{{ vmname }}
+        - r3e-{{ vmname }}
+        - "{{ hostvars[vmname].vm_network_cooked.primary.ip }}"
index 0cc0be4..49d3e16 100755 (executable)
@@ -15,9 +15,5 @@ shift
 echo "installing vm: $name with $distro/$codename"
 echo ""
 
-echo "########## clearing old ssh host keys #########"
-./remove-known-host.sh "$name"
-echo ""
-
 echo "######## running the install playbook ########"
 exec ansible-playbook -e "vmname=$name" -e "vmdistro=$distro" -e "vmdistcodename=$codename" $@ vm-install.yml
index 4a6a0b5..507906f 100644 (file)
@@ -14,6 +14,8 @@
       group: _vmhost_
   # TODO: add some sanity checks
 
+- import_playbook: remove_known_hosts.yml
+
 - name: basic installation
   hosts: _vmhost_
   roles: