Skip to content

Instantly share code, notes, and snippets.

@ilyar
Created June 8, 2017 16:45
Show Gist options
  • Save ilyar/d2ed54b671c997b86cab9d1fe2d021ab to your computer and use it in GitHub Desktop.
Save ilyar/d2ed54b671c997b86cab9d1fe2d021ab to your computer and use it in GitHub Desktop.
Find out current OS inside Vagrantfile

Add this into your Vagrantfile:

module OS
    def OS.windows?
        (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    end

    def OS.mac?
        (/darwin/ =~ RUBY_PLATFORM) != nil
    end

    def OS.unix?
        !OS.windows?
    end

    def OS.linux?
        OS.unix? and not OS.mac?
    end
end

Then you can use it as you like.

if OS.windows? [then]
    code...
end

Edit: was missing the ? on if condition.

Example used to test:

is_windows_host = "#{OS.windows?}"
puts "is_windows_host: #{OS.windows?}"
if OS.windows?
    puts "Vagrant launched from windows."
elsif OS.mac?
    puts "Vagrant launched from mac."
elsif OS.unix?
    puts "Vagrant launched from unix."
elsif OS.linux?
    puts "Vagrant launched from linux."
else
    puts "Vagrant launched from unknown platform."
end

Execute:

# Ran provision to call Vagrantfile.
$ vagrant provision
is_windows_host: false
Vagrant launched from mac.

sources: https://stackoverflow.com/a/26889312/3123272

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