Ask Your Question
1

Hiera, yaml and anchors

asked 2015-11-09 05:44:56 -0500

asktbt gravatar image

updated 2015-11-09 15:10:50 -0500

Hi,

I have a data-set in my Hiera yaml file that has a couple of repetitions I'd like to reduce. Yaml allows anchors to refer to defined data-sets. Settings this in the Yaml file in the hiera backend like below, works and hiera delivers back all values in the hash:

# yaml file
class::parameter:
  127.0.0.1:
    secret: testing 123 
    shortname: localhost
    nastype: other
  192.168.1.219:
    secret: testing123
    shortname: localbox
  10.5.128.0/24: &id001
    secret: password
    nastype: A
    shortname: private-network
  192.168.53.0/24: *id001
  192.168.57.0/24: *id001

$ hiera 'class:parameter'
{"127.0.0.1"=>
  {"secret"=>"testing 123", "shortname"=>"localhost", "nastype"=>"other"},
 "192.168.1.219"=>{"secret"=>"testing123", "shortname"=>"localbox"},
 "10.5.128.0/24"=>
  {"secret"=>"password",
   "nastype"=>"A",
   "shortname"=>"private-network"},
 "192.168.53.0/24"=>
   {"secret"=>"password",
    "nastype"=>"A",
    "shortname"=>"private-network"},
 "192.168.57.0/24"=>
   {"secret"=>"password",
    "nastype"=>"A",
    "shortname"=>"private-network"},
}

If, however, I try to overwrite a value in a referenced set, Hiera throws me an error:

# Added line in original yaml file
85.119.140.2/32: *id001
  shortname: non-private-network

# Hiera error:
$ hiera 'class:parameter'

/.rvm/gems/ruby-2.0.0-p247/gems/psych-2.0.13/lib/psych.rb:370:in `parse': (<unknown>): did not find expected key while parsing a block mapping at line 517 column 3 (Psych::SyntaxError)
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/psych-2.0.13/lib/psych.rb:370:in `parse_stream'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/psych-2.0.13/lib/psych.rb:318:in `parse'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/psych-2.0.13/lib/psych.rb:245:in `load'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend/yaml_backend.rb:19:in `block (2 levels) in lookup'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/filecache.rb:53:in `read_file'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend/yaml_backend.rb:18:in `block in lookup'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:120:in `block in datasourcefiles'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:92:in `block in datasources'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:90:in `map'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:90:in `datasources'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:115:in `datasourcefiles'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend/yaml_backend.rb:17:in `lookup'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:266:in `block (2 levels) in lookup'
from /home/jt/.rvm/gems/ruby-2.0.0-p247/gems/hiera-2.0.0/lib/hiera/backend.rb:265 ...
(more)
edit retag flag offensive close merge delete

Comments

I think I covered your case here, http://ask.puppetlabs.com/question/19711/yaml-for-puppet-users/ The problem is alias and anchors *must* be in the same file, Hiera doesn't merge yaml docs, but data from those yaml docs (assuming I've understood it correctly)

ramindk gravatar imageramindk ( 2015-11-09 13:26:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-11-09 15:08:43 -0500

asktbt gravatar image

I am gonna answer this myself. Thanks for the link, that proved to be useful.
I can confirm that. Using the merge syntax << makes it work. Obviously I didn't make it clear that the example in the original question is one yaml file. The example in Wikipedia is not working without the two left-pointing arrows in hiera.
The anchor can be defined in the same hiera hash or in a one above it, that does not seem to make any difference:

class::parameter:
  setting1: &id001
    settingA: valueA
    settingB: valueB
  setting2: 
    <<: *id001
    settingB: valueC  # Overwriting settingB

is the same as:

class::parameter::defaults: &id001
  settingA: valueA
  settingB: valueB
class::parameter:
  setting1:
    <<: *id001
  setting2:
    <<: *id001
    settingB: valueC  # Overwriting settingB

I am not sure why this syntax differs from the example in the Wikipedia.
For better clarity I think defining the default in a separate hash might be beneficial. Thank you for helping me to understand this better.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2015-11-09 05:44:56 -0500

Seen: 675 times

Last updated: Nov 09 '15