executable_ansible-update-ssh-known-hosts 503 B

123456789101112131415
  1. #!/usr/bin/env bash
  2. mapfile -t hosts < <(ansible "${1:-all}" --list-hosts | awk '! /:/ { print $NF }')
  3. for host in "${hosts[@]}"
  4. do
  5. if [[ $(timeout 3 ssh -o StrictHostKeyChecking=no "$host" -- hostname 2>&1) == *"Add correct host key"* ]]
  6. then
  7. sed -i "/$host/d" "$HOME"/.ssh/known_hosts
  8. ssh timeout 3 ssh -o StrictHostKeyChecking=no "$host" -- hostname
  9. printf "Update %s in %s file.\n" "$host" "$HOME"/.ssh/known_hosts
  10. else
  11. printf "%s\n" "$host"
  12. fi
  13. done