39

Does anyone know how to escape colons in YAML? The key in my yml is the domain with port number, but the yml file isn't working with this setup:

###BEGIN
production:
### THIS IS THE ONE I'm HAVING TROUBLE WITH ###
8.11.32.120:8000: GoogleMapsKeyforThisDomain
exampledomain.com: GoogleMapsAPIKeyforThatDomain

development:
 GoogleMapsAPIKeyforDevelopmentDomain
###END

I'm using a google maps plugin called YM4R that uses a .yml file to select the different Google Maps API key depending on where my app is being hosted...

So, I'm trying to make 8.11.32.120:8000 the key. Any idea how to do this? (It's in the gmaps_api_key.yml file if you care)

dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • 2
    Does double-quoting the whole string (ie: `"8.11.32.120:8000: GoogleMapsKeyforThisDomain"`) work as per [this StackOverflow answer](http://stackoverflow.com/q/11301650/567863)? – Paul Fioravanti Feb 14 '13 at 11:04
  • 1
    WOrks with quotes, are you telling me that is the only way to escape it? –  Feb 14 '13 at 11:09
  • There's info out there about [using double-quotes to escape colons in values](http://stackoverflow.com/q/8783705/567863), but I haven't seen anything specifically to do with escaping colons in keys apart from [a Ruby Forum post where this same question was asked back in 2007](http://www.ruby-forum.com/topic/127343), where the advice there was also to just double-quote the key. So, I guess you can either go with double quoting just the key, or both the key and value. – Paul Fioravanti Feb 14 '13 at 11:17
  • I saw that post and just wanted any other ways to do it. –  Feb 14 '13 at 11:24

2 Answers2

55

You'll need to put quotes around the key you're having trouble with. I.e.:

"8.11.32.120:8000": GoogleMapsKeyforThisDomain
Kyri Elia
  • 997
  • 9
  • 18
10

To answer a comment another way for list items like:

- sed -i "s/driver: .*/driver: pdo_$DB/" etc/config.yaml

is to write them as:

- >-
  sed -i "s/driver: .*/driver: pdo_$DB/" etc/config.yaml

or as:

- 'sed -i "s/driver: .*/driver: pdo_$DB/" etc/config.yaml'
K. Frank
  • 1,325
  • 1
  • 10
  • 19
  • For those who are wondering like I was: the `>` indicates a folded scalar, which means that characters that are otherwise special yaml characters are not interpreted. The `-` is a chomp control modifier, indicating that eventual newlines in a `>` block should be stripped. – josch Sep 21 '22 at 10:06