How to Pass Parameter on 'Vagrant Up' and Have It in the Scope of Vagrantfile

How to pass parameter on 'vagrant up' and have it in the scope of Vagrantfile?

You cannot pass any parameter to vagrant. The only way is to use environment variables

MY_VAR='my value' vagrant up

And use ENV['MY_VAR'] in recipe.

Vagrant: passing parameters in windows

So I stumbled on this issue as well. To pass parameters from command prompt to Vagrantfile it should pass as an environment variable, and you can do it in one line:

set "SERV=client" && vagrant up

In the Vagrantfile, you then can access the parameter as ENV['SERV']

A heads-up is that the environment variable will still exist in the environment after vagrant has finished.

Pass environment variables to vagrant shell provisioner

It's not ideal, but I got this to work for now:

config.vm.provision "shell" do |s|
s.inline = "VAR1 is $1 and VAR2 is $2"
s.args = "#{ENV['VAR1']} #{ENV['VAR2']}"
end

Vagrant argv input on terminal complains about machine name

The Vagrantfile is not executed directly so you cannot just pass in the arguments as you would with a normal script. vagrant looks for the file inside cwd() and bring it in.

Would go the route of the env vars or a template file which you generate before running vagrant.

How do I reuse variables in quoted line in a Vagrantfile?

It's more a question about Ruby syntax. You can use:

ansible.playbook = 'install/mydir/install_' + name + '.yml'

or (thanks to Frédéric Henri)

ansible.playbook = "install/mydir/install_#{name}.yml"

To reference the value from the hash (per OP's own suggestion):

ansible.playbook = "install/mydir/install_#{host[:name]}.yml"

How to extend a vagrant plugin through Vagrantfile without changing the .rb file?

You can use multiple provisioning sections next one another which will be executed in the order as you defined them in your Vagrantfile.

Instead of patching a plugin, try to prepare your ansible execution accordingly.

Before executing the provisioning part for ansible, execute a shell provisioning as follows:

Vagrant.configure("2") do |node|

# ...

node.vm.provision :shell, path: "fix_repo_and_add_packages.sh"

node.vm.provision "ansible_local" do |ansible|
ansible.playbook = ansible_playbook
ansible.verbose = true
ansible.install = true
## Actually this line doesn't suffice my problem since it still errs as pip requires other packages. Please check the *.rb file below
if i == 100
ansible.install_mode = "pip"
ansible.version = "2.9"
#ansible.ansible_rpm_install = Foo
end
end

end

Your fix_repo_and_add_packages.sh contains the setup of the missing repos and here you can add packages as well.

For more information about shell provisioning, you can find in the doc

vagrant provision: read line from keyboard/stdin

I do it this way, in my Vagrantfile

puts "Input text: "
input = STDIN.gets.chomp

config.vm.provision :shell, inline: "echo #{input}"


Related Topics



Leave a reply



Submit