Skip to content

Instantly share code, notes, and snippets.

@konstruktoid
Created October 26, 2020 08:59
Show Gist options
  • Save konstruktoid/c14c6b609e9e2c33c72ad5b169632f7f to your computer and use it in GitHub Desktop.
Save konstruktoid/c14c6b609e9e2c33c72ad5b169632f7f to your computer and use it in GitHub Desktop.
Generate a Ansible hosts file from running Vagrant VMs
#!/bin/bash
{
for VM in $(vagrant status | grep -iE 'running.*virtualbox' | grep "$1" | awk '{print $1}'); do
mapfile -t VAGRANT_SSH < <(vagrant ssh-config "$VM" | awk '{print $NF}')
echo "[${VAGRANT_SSH[0]}]"
echo "${VAGRANT_SSH[1]} ansible_connection=ssh ansible_port=${VAGRANT_SSH[3]} ansible_user=${VAGRANT_SSH[2]} ansible_ssh_private_key_file=${VAGRANT_SSH[7]}"
done
} > "hosts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment