Ask Your Question
1

YAML for Puppet users?

asked 2015-10-08 15:51:07 -0500

ramindk gravatar image

Is there a guide explaining some of the advanced or useful features of YAML for Puppet users?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
6

answered 2015-10-08 15:53:50 -0500

ramindk gravatar image

updated 2015-10-16 17:19:08 -0500

This is a work in progress, but wanted to get something saved for editing later.

Source Links and Tools:

which YAML lint tool

yamllint currently has better tests including one for duplicate keys. However yaml-lint handles multiple directories and files better.

Data in YAML

These are some simple examples of data structures you can create in your YAML files.

# key/value
postfix::interface:                # value is nil
postfix::interface: ''             # value is empty
postfix::interface: 'all'          # value is "all"

# arrays
postfix::relays:                   # value is nil
  -
postfix::relays: []                # value is empty
postfix::relays:                   # value is ["relay01", "relay02"]
  - 'relay01'
  - 'relay02'

# hash or dict
postfix::aliases: {}               # value is empty
postfix::aliases:                  # value is {"root"=>{"destination"=>"admin@example.com"} }
  root:
     destination: 'admin@example.com'

YAML supports basic types like booleans, integers, floats, and strings.

---
my_data_types:
  boolean: true
  float: 3.14                 
  integer: 4
  string: somewords
  stringthatneedsquotes: "Some characters like % need to be encapsulated in quotes"
  stringifiedinteger:     '7'      # value is string '7', not integer 7
  stringifiedboolean:    'false'   # value is string 'false', not boolean false.

You can manually specify the data types rather than depend on the local YAML parser. This may be useful if data is meant to be read on Puppet masters with different Ruby installs. Ruby 1.8.7 contains a different YAML parser than 1.9+. These versions have been know to disagree on how to represent YAML data. In the base below the quotes are just encapsulating.

---
specify_my_data_types:
  a_boolean: !!bool 'true'
  a_string:  !!str  true
  a_integer: !!num  '-1'

Output

hiera specify_my_data_types environment=testing   
{"a_boolean"=>true, "a_string"=>"true", "a_integer"=>-1}

Referring to Puppet facts and functions

examples of each

---
postfix::myorigin:    "%{::fqdn}"
postfix::relay:       "relay.%{::domain}"
postfix::allowed_ips: "%{hiera('my_ips')}"

Anchors and Alias example

Anchors and Aliases are part of the YAML spec. They can be of some use with Puppet and Hiera, however there are many limitations which make their use very niche.

Limitations:

  • Parse order dependent. Anchors must be first.
  • Merge is also parse order dependent. Overrides, must be after merge.
  • Anchors and Aliases must be in the same file because they are part of the YAML spec, not Hiera.

One possible use for anchors might be to set a default for a defined type within a role. In this example we're going set defaults for apache::vhost and then override them in the rest of the YAML. Because of the limitations we can only do this in one file and the defaults will not apply across the hierarchy.

hieradata/role/site_hosting.yaml

---
# The anchor, &apache_vhosts_defaults in this example
apache::vhosts::defaults: &apache_vhosts_defaults
  email:      'default_admin@example.com'
  servername: 'localhost'

# Anchors must be defined before they are referenced
# parser order matters
apache::vhosts:
  demo.example.com:
    <<: *apache_vhosts_defaults  # the <<: mean merge the hash
    port: '8088'
  test.example.com:
    <<: *apache_vhosts_defaults 
    email: 'reza@example.com' # NOTE: parser order, this will overwrite the default
    port: '8080'
  prod.example.com:
    email: 'prod@example.com'  # NOTE: The merge happena ...
(more)
edit flag offensive delete link more

Comments

very nice!

sahumphries gravatar imagesahumphries ( 2015-10-09 06:29:23 -0500 )edit

Nice! Were you still planning to put a reference project on github?

rnelson0 gravatar imagernelson0 ( 2015-10-20 16:28:24 -0500 )edit
0

answered 2015-10-10 14:03:51 -0500

lupin gravatar image

You might want to add this, I found it useful in rare cases.

Empty Hash and Array on YAML

---
empty_hash: {}
empty_array: []

Multi-line block

---
multi_line1: |
   line1 first line
   line2 second line

multi_line2: >
   line1 first line
   line2 second line
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

1 follower

Stats

Asked: 2015-10-08 15:51:07 -0500

Seen: 985 times

Last updated: Oct 16 '15