Monday, September 20, 2021

Vagrant tips: How to automatically adjust the Time Zone of your vagrant box

This image has an empty alt attribute; its file name is vagrant_tz.png


I was playing with my Linux vagrant boxes lately and I realized I was always spinning third party boxes that had a totally different time zone (Europe/UK mostly). Up until now I never bothered as I have used bunch of those from vagrant Cloud for years. I sometimes notice, and think of using a shell command to fix it during the bootstrap, but it I haven’t had the time. However, I was heavily debugging some logs this week and the wrong Time Zone started to get annoying for me so I decided to look for a permanent fix. 

I. Vagrant Time zone plugin to the rescue 

  • Solution

    There is no need to add random shell script in the shell provisioning area of your Vagrantfile.Teemu Matilainen has you covered with his sweet ruby plugin called  vagrant-timezone that does just that.

  • How does it work
    Install the plugin:
  • C:\Users\brokedba> vagrant plugin install vagrant-timezone
    Installing the 'vagrant-timezone' plugin. This can take a few minutes...
    Fetching vagrant-timezone-1.3.0.gem
    Installed the plugin 'vagrant-timezone (1.3.0)'!


    To Configure time zone for all Vagrant VMs, add the following to $HOME/.vagrant.d/Vagrantfile or to a project specific Vagrantfile (see below example in windows)

    C:\Users\brokedba\.vagrant.d>vagrant init
    --- Add the IF block below in the master or each build’s Vagrantfile

    .configure("2") do |config| if Vagrant.has_plugin?("vagrant-timezone") config.timezone.value = "America/Toronto" end # ... other stuff end

    You can of course choose your own Time Zone from this TZ database list   

    - The configuration is done on vagrant up and vagrant reload actions.
    Note that no services are restarted automatically so they may keep using the old time zone information. 

    - This plugin requires Vagrant 1.2 or newer.


II. Usage and prerequisites

III. Compatibility

  • Linux guests.
    • Arch
    • CoreOS
    • Debian (and derivatives)
    • Gentoo
    • RedHat (and derivatives)
  • BSD guests:

    • FreeBSD
    • NetBSD
    • OpenBSD
    • OS X
  • Windows


This will hopefully encourage you to more often use and enjoy vagrant boxes without being stuck with the original time zone .

Thanks for reading

No comments:

Post a Comment