querying hiera data without providing host/scope

408 views
Skip to first unread message

Andreas Paul

unread,
Apr 30, 2012, 9:52:00 AM4/30/12
to puppet...@googlegroups.com
Hello there,

I need to access the following hiera data in a script:
$ hiera -c /etc/puppet/hiera.yaml tomcats environment=development line=intra3
["tc7_test1", "tc7_test8"]
or
$ hiera -c /etc/puppet/hiera.yaml apache_port environment=development line=intra3 name=tc7_test1
81

The example on https://github.com/puppetlabs/hiera uses facts of a specific hosts.
I don't want to specify and load each YAML file for each host, because the hosts are already defined for a specific line and stage environment:
$ hiera -c /etc/puppet/hiera.yaml hosts environment=development line=intra3
["serverA.domain.tld", "serverB.domain.tld"]

But I can't get the hiera.lookup to work without providing a scope with the facts of a specific host.

I know that I could just call hiera and parse the output, but I thought I better ask before doing that.
Maybe I'm even using environment and stage specific hiera data the wrong way.

Here is my current hiera config:

$ cat /etc/puppet/hiera.yaml
:hierarchy:
    - %{environment}/%{line}/%{name}
    - %{environment}/%{line}
    - %{environment}
:backends:
    - yaml
:yaml:
    :datadir: '/etc/puppet/environments/%{environment}/hieradata'
:puppet:
    :datasource: data
$ hiera -c /etc/puppet/hiera.yaml apache_port environment=development line=intra3
80
$ hiera -c /etc/puppet/hiera.yaml apache_port environment=development line=intra3 name=tc7_test1
81
$ hiera -c /etc/puppet/hiera.yaml apache_port environment=development line=intra3 name=tc7_test8
80
$ cat /etc/puppet/environments/development/hieradata/development.yaml
---
# default values for this stage
apache_port: '80'

$ cat /etc/puppet/environments/development/hieradata/development/intra3.yaml
---
# line specific values
hosts:  - 'serverA.domain.tld'
        - 'serverB.domain.tld'
tomcats:  - 'tc7_test1'
          - 'tc7_test8'

$ cat /etc/puppet/environments/development/hieradata/development/intra3/tc7_test1.yaml
---
# tc7_test1 specific values
apache_port: 81


Thanks in advance!

R.I.Pienaar

unread,
Apr 30, 2012, 11:32:26 AM4/30/12
to puppet...@googlegroups.com


----- Original Message -----
> From: "Andreas Paul" <xor...@googlemail.com>
> To: puppet...@googlegroups.com
> Sent: Monday, April 30, 2012 2:52:00 PM
> Subject: [Puppet Users] querying hiera data without providing host/scope
>
> Hello there,
>
> I need to access the following hiera data in a script:
> $ hiera -c /etc/puppet/hiera.yaml tomcats environment=development
> line=intra3
> ["tc7_test1", "tc7_test8"]
> or
> $ hiera -c /etc/puppet/hiera.yaml apache_port environment=development
> line=intra3 name=tc7_test1
> 81
>
> The example on https://github.com/puppetlabs/hiera uses facts of a
> specific hosts.
> I don't want to specify and load each YAML file for each host,
> because the hosts are already defined for a specific line and stage
> environment:
> $ hiera -c /etc/puppet/hiera.yaml hosts environment=development
> line=intra3
> ["serverA.domain.tld", "serverB.domain.tld"]
>
> But I can't get the hiera.lookup to work without providing a scope
> with the facts of a specific host.

In this case the scope is just a hash so the cli command:

$ hiera -c /etc/puppet/hiera.yaml hosts environment=development line=intra3

translates to:

hiera.lookup("hosts", nil, {"environment" => "development", "line" => "intra3"})

Andreas Paul

unread,
May 2, 2012, 4:36:56 AM5/2/12
to puppet...@googlegroups.com
Ah, exactly what I needed!
Thanks!
Reply all
Reply to author
Forward
0 new messages