ajhfleuwr ajhfleuwr - 4 months ago 14
Bash Question

How can I do computations on the fly within the vagrant file

How can I compute the hash on the fly within the vagrant file based on some input, for e.g. I do

# shell
$ echo "machine13" | sha256sum | cut -c1-12
2372240456c2

# vagrantfile
Vagrant.configure(2) do |config|
config.vm.hostname = "machine13"
config.vm.network "public_network", bridge: "eth0", use_dhcp_assigned_default_route: true, :mac => "2372240456c2"
end

Answer

If you just want to run a bash command and get its output to a variable in your Vagrantfile, you could use the ruby's back ticks to execute the command.

For example, your Vagrantfile:

# Vagrantfile
my_hostname = "machine12"
mac_hash = `echo #{my_hostname} | sha256sum | cut -c1-12`

Vagrant.configure(2) do |config|
  config.vm.hostname = my_hostname
  config.vm.network "public_network", bridge: "eth0", use_dhcp_assigned_default_route: true,  :mac => mac_hash
end
Comments