Search code examples
yamlpuppethiera

Is there a better way of handling double quotes hiera to yaml


I am using a hiera to keep environment/host specific configuration away from code. For generating configuration file - yaml I bump into following scenario:

Hieara config file:

elasticsearch::discovery_unicast_hosts: [ "HOSP-BD-02", "HOSP-BD-03", "HOSP-BD-04" ]

In generate yaml config appears:

discovery.zen.ping.unicast.hosts: [HOSP-BD-02,HOSP-BD-03,HOSP-BD-04]

and trouble is missing double quotes around every single item in the array. I did following processing of the array before inserting to erb template:

 $discovery_unicast_hosts_joined = join($elasticsearch::discovery_unicast_hosts, ",")

If I insert array directly

 ["HOSP-BD-02""HOSP-BD-03""HOSP-BD-04"]

quotes are there but missing comas. One simple and nasty stolution would be escape " in hieara what I am trying to avoid because it breaks consistency and is error prone.

elasticsearch::discovery_unicast_hosts: [ "\"HOSP-BD-02\"", "\"HOSP-BD-03\"", "\"HOSP-BD-04\"" ]

Is there a better way to solve this?


Solution

  • Your join approach is correct, but your values lack quotes.

    The cleanest approach would be to make sure that your YAML includes actual quotes in the data (in your current notation, YAML will consider the quotes as syntactic sugar).

    If this is distasteful to you (which I could get behind), you can manipulate the data further during processing, using the regsubst function.

    $quoted = regsubst($elasticsearch::discovery_unicast_hosts, '(.*)', '"\1"')
    $discovery_unicast_hosts_joined = join($quoted, ",")