Skip to content

Instantly share code, notes, and snippets.

@mathieulegrand
Created April 4, 2014 02:03
Show Gist options
  • Save mathieulegrand/9966710 to your computer and use it in GitHub Desktop.
Save mathieulegrand/9966710 to your computer and use it in GitHub Desktop.
Vagrant workaround to set hostname with vSphere provider
#!/bin/sh
# -- tested with Redhat Enterprise Linux 6.4
FQDN="$1"
/bin/echo "Update sysconfig"
/bin/sed -i "s/\\(HOSTNAME=\\).*/\\1${FQDN}/" /etc/sysconfig/network
/bin/echo "Update hostname"
/bin/hostname ${FQDN}
SHORT_HOSTNAME=`hostname -s`
/bin/echo "Update /etc/hosts"
IPADDR=$(/sbin/ifconfig | /bin/grep 'inet addr:'| /bin/grep -v '127.0.0.1' | /bin/cut -d: -f2 | /bin/awk '{ print $1}')
# remove lines containing my IP address from /etc/hosts
/bin/sed -i "/^$IPADDR/ d" /etc/hosts
# remove lines containing my hostname from /etc/hosts
/bin/sed -i "/$SHORT_HOSTNAME/ d" /etc/hosts
# add my ip address and hostname (fqdn simple in this order) to /etc/hosts
/bin/echo "$IPADDR $FQDN $SHORT_HOSTNAME" >> /etc/hosts
/bin/echo "Update dhcp configuration"
/bin/sed -i "/DHCP_HOSTNAME=/ d" /etc/sysconfig/network-scripts/ifcfg-*
for f in [ /etc/sysconfig/network-scripts/ifcfg-* ]; do
case "$f" in
*lo)
/bin/echo "Nothing to do for $f"
;;
*)
/bin/echo "DHCP_HOSTNAME=$SHORT_HOSTNAME" >> $f
;;
esac
done
/sbin/service network restart
exit 0
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# [...]
config.vm.hostname = 'my-hostname.fully.qualified'
config.vm.provider :vsphere do |vsphere|
# [...]
end
# workaround vsphere provider not setting hostname
config.vm.provision "shell" do |s|
s.path = "scripts/set-hostname.sh"
s.args = [ config.vm.hostname ]
end
# other provisionner goes here, eg:
config.vm.provision "ansible" do |ansible|
ansible.playbook = "site.yml"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment