Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use variables in a YAML file?

I have YAML file which needs to take a variable as an input:

outputters:
    - type        : DateFileOutputter
      name        : logfile
      level       : DEBUG
      date_pattern: '%Y%m%d'
      trunc       : 'false'
      dirname     : "/home/sameera/workspace/project/log"
      filename    : "message.log"
      formatter   :
        date_pattern: '%m/%d/%Y %H:%M:%S'
        pattern     : '%d %l - %m'
        type        : PatternFormatter

I want to pass dirname as a parameter, something like:

      dirname     : "<%= LOGFILE_PATH%>"

My LOGFILE_PATH is defined in a file called init.rb.

like image 732
sameera207 Avatar asked Jan 04 '12 06:01

sameera207


People also ask

Can you use variables in YAML files?

In YAML, you can only define variables with pipeline or action scope. Workspace and project variables need to be defined via the GUI.

How do I pass a variable in YAML file?

Passing variables between tasks in the same job For example, to pass the variable FOO between scripts: Set the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.

Does YAML have data types?

YAML has three types of data types: Scalar. List. Dictionary.


1 Answers

You could use ERB.

For example:

template = ERB.new File.new("path/to/config.yml.erb").read
processed = YAML.load template.result(binding)

You can read more on binding here: ruby metaprogramming.

like image 151
Sergio Tulentsev Avatar answered Sep 25 '22 07:09

Sergio Tulentsev