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

后端 未结 5 2117
暗喜
暗喜 2020-12-12 12:40

I\'m looking for a way to pass parameter to Chef cookbook like:

$ vagrant up some_parameter

And then use some_parameter inside

5条回答
  •  悲&欢浪女
    2020-12-12 13:23

    It is possible to read variables from ARGV and then remove them from it before proceeding to configuration phase. It feels icky to modify ARGV but I couldn't find any other way for command-line options.

    Vagrantfile

    # Parse options
    options = {}
    options[:port_guest] = ARGV[1] || 8080
    options[:port_host] = ARGV[2] || 8080
    options[:port_guest] = Integer(options[:port_guest])
    options[:port_host] = Integer(options[:port_host])
    
    ARGV.delete_at(1)
    ARGV.delete_at(1)
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      # Create a forwarded port mapping for web server
      config.vm.network :forwarded_port, guest: options[:port_guest], host: options[:port_host]
    
      # Run shell provisioner
      config.vm.provision :shell, :path => "provision.sh", :args => "-g" + options[:port_guest].to_s + " -h" + options[:port_host].to_s
    

     

    provision.sh

    port_guest=8080
    port_host=8080
    
    while getopts ":g:h:" opt; do
        case "$opt" in
            g)
                port_guest="$OPTARG" ;;
            h)
                port_host="$OPTARG" ;;
        esac
    done
    

提交回复
热议问题