Conditionally call another position in Vagrantfile

I have the following settings in my strollers file.

config.vm.provision :shell, :path => "provision/bootstrap.sh" config.vm.provision :shell, :path => "provision/step-1.sh" config.vm.provision :shell, :path => "provision/step-2.sh" config.vm.provision :shell, :path => "provision/dev-setup.sh" 

provision/bootstrap.sh needs to be started always, however I need to conditionally start the remaining conditions. E.g. if the mode is dev , run provision/dev-setup.sh

Is there a built-in Vagrant configuration setting for this? (e.g. pass args command line to vagrant provision )?

I would not want to rely on ENV variables, for example, if possible.

+5
source share
2 answers

I believe that environment variables are the most common way to handle this, and there is no way to pass anything through the vagrant up or vagrant provision . Here are some alternative ideas you might want to explore:

  • There is something else that is different from the Dev and Prod environments. Vagrantfile is just a Ruby script, so anything that can be discovered can be used to control the sequence of the script. For example, the presence / absence of a file, local network, host name, etc.

  • Identify individual Vagrant nodes that are actually the same, but differ depending on the provision. For example, with a file like the one below, you can do vagrant up prod or vagrant up dev , depending on your environment:

     Vagrant.configure("2") do |config| config.vm.provision :shell, :path => "provision/bootstrap.sh" config.vm.provision :shell, :path => "provision/step-1.sh" config.vm.provision :shell, :path => "provision/step-2.sh" config.vm.define "prod" do |prod| ... end config.vm.define "dev" do |dev| ... config.vm.provision :shell, :path => "provision/dev-setup.sh" end end 
+5
source

You can also create a new variable in your inventory for storing the environment, for example env: dev in the inventory dev and env: production in the prod inventory, and then use this to conditionally include your individual task files, for example:

 # provision.yml (your main playbook) - include: provision/bootstrap.sh - include: provision/step-1.sh - include: provision/step-2.sh - include: provision/dev-setup.sh when: env == 'dev' 

Then in Vagrant you can specify the inventory to use, for example:

 config.vm.provision "provision", type: "ansible" do |ansible| ansible.playbook = 'provision.yml' ansible.inventory_path = 'inventory/dev' end 

Additional Information:

+1
source

Source: https://habr.com/ru/post/1214976/


All Articles