Maurício Linhares
Maurício Linhares

Reputation: 40333

How to prevent Ruby's YAML parser from trying to parse {{var-name}}

I have a bunch of concourse pipeline files that look like the following:

---
resources:
  - name: example
    type: git
    source:
      uri: [email protected]:me/example.git
      branch: {{tracking_branch}}
      private_key: {{ssh_key}}
      paths:
        - code/src/do/teams/sampleapp
      params:
        depth: 1

  - name: deploy-image
    type: docker-image
    source:
      repository: {{docker_image_url}}

And I want to parse them in ruby to perform a bunch of transformations (like validating them and updating some keys if they are missing).

Problem is, whenever I try to load and them dump them back to files the pieces that have {{something}} become:

branch:
  ? tracking_branch: 
  : 
private_key:
  ? ssh_key: 
  : 

Why is it doing this and is there any way I can configure the parser not to do this? Just leave these variables as they are?

Upvotes: 2

Views: 68

Answers (3)

Maurício Linhares
Maurício Linhares

Reputation: 40333

Completely forgot that concourse now offers ((var-name)) for templating, just switched to that instead of {{var-name}} at the pipelines and the YAML parser is now happy!

Upvotes: 0

matt
matt

Reputation: 79743

The { and } characters are used in Yaml for flow mappings (i.e. hashes). If you don’t provide a value for a mapping entry you get nil.

So in the case of branch: {{tracking_branch}}, since there are two pairs of braces, you get a hash with a key branch and value (in Ruby) of

{{"tracking_branch"=>nil}=>nil}

When this is dumped back out to Yaml you get the somewhat awwkward and verbose:

branch:
  ? tracking_branch:
  :

The solution is simply to quote the value:

branch: "{{tracking_branch}}"

Upvotes: 1

tadman
tadman

Reputation: 211610

To avoid conflict with YAML's internal syntax you need to quote your values:

---
resources:
  - name: example
    type: git
    source:
      uri: [email protected]:me/example.git
      branch: '{{tracking_branch}}'
      private_key: '{{ssh_key}}'
      paths:
        - code/src/do/teams/sampleapp
      params:
        depth: 1

This sort of thing comes up in Ansible configuration files all the time for similar reasons.

Upvotes: 1

Related Questions