Hiera isn't merging hashes

1,802 views
Skip to first unread message

paul....@complex.com

unread,
Feb 26, 2014, 5:19:23 PM2/26/14
to puppet...@googlegroups.com
I have been doing some experimenting with Puppet + Hiera with some of my hashes, but when it is going through my hierarchy it is returning whatever hash it hits first and skipping the others.

For my node 'sandbox1'

sandbox.pp:

node /^sandbox\d+/ {
  class { 'php':}
  notify { 'PHP hash':
    message => hiera('php::augeas',{})
  }
  $php_settings = hiera('php::augeas',{})
  create_resources('php::augeas',$php_settings)
}

hiera.yaml:

---
:backends:
  - json
:json:
  :datadir: /etc/puppet/hiera
:merge_behavior: "deeper"
:hierarchy:
  - "nodes/%{::hostname}"
  - "roles/%{::role}"
  - "provider/%{::provider}"
  - "provider/%{::provider}/virtual/virtual_%{::is_virtual}"
  - common

hiera/nodes/sandbox1.json:
{
    "php::augeas" : {
        "php-allow_url_fopen" : {
            "entry"   : "PHP/allow_url_fopen",
            "value"   : "On"
        }
    }
}

hiera/roles/sandbox.json:
{
    "php::augeas" : {
        "php-log_errors" : {
            "entry"   : "PHP/log_errors",
            "value"   : "On"
        }
    }

hiera/common.json:
{
    "php::augeas" : {
        "php-date_timezone" : {
            "entry"   : "Date/date.timezone",
            "value"   : "America/New_York"
        },
        "php-short_open_tag" : {
            "entry"   : "PHP/short_open_tag",
            "value"   : "On"
        },
        "php-error_log" : {
            "entry"   : "PHP/error_log",
            "value"   : "/var/log/php/php_error.log"
        }
    }

In this configuration, only  "php-allow_url_fopen" from the top-level nodes hash is returned. If I remove that nodes/sandbox1.json, it only returns the "php-log_errors" from the next "roles" hiera level down. Finally, if the nodes and roles json's are removed it will apply all three from the common.json hash.

I have tried both native and deeper merge behaviors with the same result. The strangest thing is that the hiera command line tool on the master returns the correct hash:

# hiera -h php::augeas ::hostname=sandbox1 ::role=sandbox -c /etc/puppet/hiera.yaml

{"php-date_timezone"=>
  {"entry"=>"Date/date.timezone", "value"=>"America/New_York"},
 "php-short_open_tag"=>{"entry"=>"PHP/short_open_tag", "value"=>"On"},
 "php-error_log"=>
  {"entry"=>"PHP/error_log", "value"=>"/var/log/php/php_error.log"},
 "php-log_errors"=>{"entry"=>"PHP/log_errors", "value"=>"On"},
 "php-allow_url_fopen"=>{"entry"=>"PHP/allow_url_fopen", "value"=>"On"}}

Andrey Kozichev

unread,
Feb 26, 2014, 6:08:51 PM2/26/14
to puppet...@googlegroups.com

you need to use hiera 1.2 and enable deep merging

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/96454179-6cd7-4c54-9ec2-2bf553e319a9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

paul....@complex.com

unread,
Feb 26, 2014, 10:07:00 PM2/26/14
to puppet...@googlegroups.com
I currently have these versions of puppet related packages installed:

# yum list installed | grep puppet

facter.x86_64                   1:1.7.3-1.el6          @puppetlabs-products
hiera.noarch                    1.3.1-1.el6            @puppetlabs
puppet.noarch                   3.3.1-1.el6            @puppetlabs
puppet-dashboard.noarch         1.2.23-1.el6           @puppetlabs-products
puppet-server.noarch            3.3.1-1.el6            @puppetlabs
puppetdb.noarch                 1.5.2-1.el6            @puppetlabs
puppetdb-terminus.noarch        1.5.2-1.el6            @puppetlabs

I also have :merge_behavior set to deeper

Charlie Sharpsteen

unread,
Feb 26, 2014, 10:08:27 PM2/26/14
to puppet...@googlegroups.com
On Wednesday, February 26, 2014 2:19:23 PM UTC-8, paul....@complex.com wrote:
I have been doing some experimenting with Puppet + Hiera with some of my hashes, but when it is going through my hierarchy it is returning whatever hash it hits first and skipping the others.

For my node 'sandbox1'

sandbox.pp:

node /^sandbox\d+/ {
  class { 'php':}
  notify { 'PHP hash':
    message => hiera('php::augeas',{})
  }
  $php_settings = hiera('php::augeas',{})
  create_resources('php::augeas',$php_settings)
}


The `hiera` lookup function only returns the first key it finds --- which is the behavior you are observing. For hash merge lookup, you will need to use the `hiera_hash` function in your manifest instead of `hiera`.

For full details, see the docs on Hiera lookup functions:


Hope this helps!

-Charlie

paul....@complex.com

unread,
Feb 27, 2014, 10:14:31 AM2/27/14
to puppet...@googlegroups.com
That explains a lot, thanks!

Leadel Ngalame

unread,
Aug 11, 2017, 7:02:52 AM8/11/17
to Puppet Users
Hey @Charlie, thanks for this post. I had been looking for this functionality for a while now. It works like a charm

Henrik Lindberg

unread,
Aug 11, 2017, 7:27:41 AM8/11/17
to puppet...@googlegroups.com
On 09/08/17 14:01, Leadel Ngalame wrote:
> Hey @Charlie, thanks for this post. I had been looking for this
> functionality for a while now. It works like a charm
>

Note that you found very old information. While it still works you want
to read the latest documentation for hiera
https://docs.puppet.com/hiera/3.3/puppet.html and note that from Puppet
4.9 and onwards the hiera 3 version is deprecated in favor for hiera 5.
The new version (5) resides in puppet itself.

Instead of using the hiera_xxx functions you should use the lookup()
function.

Just wanted to let you know to avoid disappointment when you have to change.

Best,
- henrik

> Am Donnerstag, 27. Februar 2014 04:08:27 UTC+1 schrieb Charlie Sharpsteen:
>
> On Wednesday, February 26, 2014 2:19:23 PM UTC-8,
> paul....@complex.com wrote:
>
> I have been doing some experimenting with Puppet + Hiera with
> some of my hashes, but when it is going through my hierarchy it
> is returning whatever hash it hits first and skipping the others.
>
> For my node 'sandbox1'
>
> sandbox.pp:
>
> node /^sandbox\d+/ {
> class { 'php':}
> notify { 'PHP hash':
> message => hiera('php::augeas',{})
> }
> $php_settings = hiera('php::augeas',{})
> create_resources('php::augeas',$php_settings)
> }
>
>
>
> The `hiera` lookup function only returns the first key it finds ---
> which is the behavior you are observing. For hash merge lookup, you
> will need to use the `hiera_hash` function in your manifest instead
> of `hiera`.
>
> For full details, see the docs on Hiera lookup functions:
>
> http://docs.puppetlabs.com/hiera/1/puppet.html#hiera-lookup-functions <http://docs.puppetlabs.com/hiera/1/puppet.html#hiera-lookup-functions>
>
> Hope this helps!
>
> -Charlie
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/b3d6fca1-711e-406c-a089-37f28b2dde8a%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/b3d6fca1-711e-406c-a089-37f28b2dde8a%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Reply all
Reply to author
Forward
0 new messages