From 53b7c937716d3ef523874a4cb3812fa8ba86dc2b Mon Sep 17 00:00:00 2001 From: nicoo Date: Thu, 14 Jun 2018 00:18:51 +0200 Subject: [PATCH] vm-install: Remove hosts from known hosts in the playbook This avoid relying on localconfig being used. --- ansible/remove-known-host.sh | 15 --------------- ansible/remove_known_hosts.sh | 13 +++++++++++++ ansible/remove_known_hosts.yml | 11 +++++++++++ ansible/vm-install.sh | 4 ---- ansible/vm-install.yml | 2 ++ 5 files changed, 26 insertions(+), 19 deletions(-) delete mode 100755 ansible/remove-known-host.sh create mode 100755 ansible/remove_known_hosts.sh create mode 100644 ansible/remove_known_hosts.yml diff --git a/ansible/remove-known-host.sh b/ansible/remove-known-host.sh deleted file mode 100755 index 2c5fd7f..0000000 --- a/ansible/remove-known-host.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -if [ -z "$1" ]; then - echo "$0 " - 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 index 0000000..ee4a0fb --- /dev/null +++ b/ansible/remove_known_hosts.sh @@ -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 index 0000000..5491342 --- /dev/null +++ b/ansible/remove_known_hosts.yml @@ -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 }}" diff --git a/ansible/vm-install.sh b/ansible/vm-install.sh index 0cc0be4..49d3e16 100755 --- a/ansible/vm-install.sh +++ b/ansible/vm-install.sh @@ -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 diff --git a/ansible/vm-install.yml b/ansible/vm-install.yml index 4a6a0b5..507906f 100644 --- a/ansible/vm-install.yml +++ b/ansible/vm-install.yml @@ -14,6 +14,8 @@ group: _vmhost_ # TODO: add some sanity checks +- import_playbook: remove_known_hosts.yml + - name: basic installation hosts: _vmhost_ roles: -- 1.7.10.4