Search code examples
rubyvagrantyamlvagrantfile

How to use YAML files with Vagrant?


I'm trying to improve my YAML file for my Vagrant project. According to this post, if I have something like this:

en:
  site_name: "Site Name"
  static_pages:
    company:
      description: ! '%{site_name} is an online system'

I should be able to print "Site Name is an online system", but I don't know how to use it inside my Vagrantfile. I tried so far but I couldn't print it out properly, just this:

%{site_name} is an online system

This is how I'm using it:

require 'yaml'
set = YAML.load_file(ENV['DEVOPS_HOME'] + '/vagrant/server/settings.yml')
puts set['en']['static_pages']['company']['description']

Solution

  • as they say in the answer of the post

    and then call in the appropriate view with the site name as a parameter

    so you don't get directly after you load the yaml file the expected string, you need to match the parameter. one way you could work this in your Vagrantfile is

    require 'yaml'
    set = YAML.load_file(ENV['DEVOPS_HOME'] + '/vagrant/server/settings.yml')
    str = set['en']['static_pages']['company']['description']
    arg = {:site_name=> set['en']['site_name']}
    p str % arg
    

    will print out "Site Name is an online system"

    The other way would be to use the answer from mudasobwa which is also detailed in the original post you reference