Ask Your Question
0

hiera command line fetch particular value

asked 2016-03-30 06:41:10 -0500

karthikeayan gravatar image

updated 2016-03-30 06:41:52 -0500

I have data in hiera like below,

nodes:
  - hostname: lb01.example.com
    ipaddress: 192.168.22.21
  - hostname: lb02.example.com
    ipaddress: 192.168.22.28

Is it possible to get the IP Address of node lb01.example.com through hiera command line?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-03-30 14:59:27 -0500

lavaman gravatar image

You would only be able to get the array of hashes. Hiera can't give you a data value based on another value. You can get that array of hashes in yaml or json format then select the value you need using jq or a script of some sort.

edit flag offensive delete link more
1

answered 2016-03-31 16:34:54 -0500

reesek gravatar image

Yes -- this is possible. I believe you need to be on at least hiera 2.0, though I tested on 3.0.3.

puppetmaster ~ # hiera --version
3.0.3

puppetmaster ~ # pwd
/etc/puppetlabs/code/environments/dev/hieradata/calling_module/dev

puppetmaster ~ # cat nodes.yaml 
nodes:
  - hostname: lb01.example.com
    ipaddress: 192.168.22.21
  - hostname: lb02.example.com
    ipaddress: 192.168.22.28

puppetmaster ~ # hiera nodes.0.ipaddress environment=dev calling_module=nodes
192.168.22.21
edit flag offensive delete link more

Comments

My understanding was that he specifically wanted to query by hostname, not by array position.

lavaman gravatar imagelavaman ( 2016-03-31 17:02:32 -0500 )edit

Ah -- could be. I see your perspective to the question. Looks like you also see mine :)

reesek gravatar imagereesek ( 2016-03-31 18:00:08 -0500 )edit

Indeed. It just forces you into constraints around data ordering and whatnot. Would be fragile to rely upon for anything important.

lavaman gravatar imagelavaman ( 2016-03-31 18:16:20 -0500 )edit

Just like anything else, it likely depends on the use case. If the user is looking to populate something like /etc/hosts from that data set for example, then maybe not. Otherwise, a restructuring of the data may be needed to avoid awkward constraints as you've pointed out.

reesek gravatar imagereesek ( 2016-03-31 21:03:18 -0500 )edit

my hiera is only 1.3.4, couldn't use your command

karthikeayan gravatar imagekarthikeayan ( 2016-04-06 07:40:36 -0500 )edit

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: 2016-03-30 06:41:10 -0500

Seen: 82 times

Last updated: Mar 31 '16