moluzhui
moluzhui

Reputation: 1093

makefile pass variable to vagrantfile and start the vagrant VM

I'm writing the automated deployment plan for the project. I plan to use makefile to control the vagrant vm and its operations. I want to place all user option configurations in the makefile, including some vagrantfile configuration parameters, such as CPU, IP, and the like. But how do I pass makefile parameters to vagrantfile? I test to use shell

CPU_NUM ?= 3

init:
    vagrant_cpu= $(CPU_NUM ) vagrant up

But I didn't know vagrantfile how to obtain it

I would very appreciate it if you guys can tell me how to achieve it that parameters are passed from the makefile to vagrantfile

Upvotes: 0

Views: 796

Answers (1)

Aurel Bílý
Aurel Bílý

Reputation: 7963

Makefile is nice, but why not just use shell scripts for general-purpose scripting? But, to actually answer your question:

The command where you set the environment variable is wrong, the = must not have a space on either side. Change it to:

init:
    vagrant_cpu=$(CPU_NUM) vagrant up

A Vagrantfile is just a Ruby script. This means that you can access environment variables the same way you would in Ruby, using ENV. In your Vagrantfile, you could have something like:

config.vm.provider "virtualbox" do |v|
  v.memory = 1024
  v.cpus = ENV["vagrant_cpu"].to_i
end

Note to_i to convert to integer.

Upvotes: 1

Related Questions