Skip to content

Instantly share code, notes, and snippets.

@pollardld
Forked from ilyar/Vagrantfile.md
Last active January 22, 2024 21:49
Show Gist options
  • Save pollardld/d15e1c38afe36ade910569a0b2cfce97 to your computer and use it in GitHub Desktop.
Save pollardld/d15e1c38afe36ade910569a0b2cfce97 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