Skip to content

Instantly share code, notes, and snippets.

@mynameisrufus
Last active July 15, 2016 19:31
Show Gist options
  • Save mynameisrufus/8dd118a396f427b34ce7 to your computer and use it in GitHub Desktop.
Save mynameisrufus/8dd118a396f427b34ce7 to your computer and use it in GitHub Desktop.
Vagrant dynamic port mapping
---
- mapping: '80:8082'
role: nil
- mapping: '53:5350/udp,53:5350,8081'
role: 'powerdns'
- mapping: '2003,4444/udp,9200,9300'
role: 'logging'
- mapping: '4000,4560,5000'
role: 'metrics'
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
# Load mappings.yml
MAPPINGS = YAML.load_file('mappings.yml')
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
MAPPINGS.each do |maps|
maps["mapping"].split(/,/).each do |mapping|
guest, host = mapping.split(/:/)
host, protocol = (host || guest).to_s.split(/\//)
config.vm.network :forwarded_port, guest: guest.gsub(/\/.*/, ''), host: (host || guest), protocol: (protocol || "tcp")
end
end
end
@mynameisrufus
Copy link
Author

I recommend checking in a mappings.example.yml and adding mappings.yml to your .gitignore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment