-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_vagrant_inventory.sh
executable file
·39 lines (35 loc) · 1.37 KB
/
init_vagrant_inventory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
#
# Set up an inventory for the Vagrant test machine.
#
# Usage: init_vagrant_inventory.sh [inventory-file]
#
# Make sure that the virtual machine is active before you execute this file.
#
# If the given inventory exists, try to replace the ansible host IP, Port and IdentityFile for the
# 'vagrant' machine with the IP, Port and IdentityFile of the VM. If the file does not exist set
# up a new one.
# The inventory file must have a server for acme. A dummy entry is fine.
#
# If no inventory file is given, use 'vagrant.ini'.
CONFIG=${2:-vagrant.ini}
VAGRANTCFG=`vagrant ssh-config $1`
SSH_IP=`echo "$VAGRANTCFG" | grep HostName | sed 's:.*HostName *::'`
SSH_Port=`echo "$VAGRANTCFG" | grep Port | sed 's:.*Port *::'`
SSH_IdentityFile=`echo "$VAGRANTCFG" | grep IdentityFile | sed 's:.*IdentityFile *::'`
if [[ -f $CONFIG ]]; then
# just make sure the IP address, Port and IdentityFile is set correctly
sed -i "s:vagrant ansible_host=[0-9.]*:vagrant ansible_host=$SSH_IP:" $CONFIG
sed -i "s:ansible_port=[0-9]*:ansible_port=$SSH_Port:" $CONFIG
sed -i "s:ansible_ssh_private_key_file=.*:ansible_ssh_private_key_file=$SSH_IdentityFile:" $CONFIG
else
# create a new inventory file for vagrant
cat > $CONFIG <<EOF
[servers]
vagrant ansible_host=$SSH_IP ansible_port=$SSH_Port ansible_ssh_private_key_file=$SSH_IdentityFile
[acme]
dummy
[your test group]
vagrant
EOF
fi