About | FAQ | Help
Ask Your Question

How To Get Access Facter Hash Key in Puppet?

asked 2017-01-12 01:34:21 -0500

gk6565 gravatar image

How To Get Access Facter Hash Key in Puppet?

I am creating a manifest to get an Agent's partition name.

The fact $partitions shows the detail of the partition info.

{"xvda1"=>{"uuid"=>"d1697425-49d0-4c9f-9901-5f9260be8196", "size"=>"83859300", "mount"=>"/", "label"=>"cloudimg-rootfs", "filesystem"=>"ext4"}}

But, I just want to get the name part (xvda1) and use it as a variable for a configuration file.

Is there any way to filter the output in Puppet?

Any help would be appreciable. Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-01-12 12:07:01 -0500

DarylW gravatar image

updated 2017-02-27 13:54:23 -0500

You are able to access the sections of a hash using keys.

In the example at https://docs.puppet.com/puppet/4.8/langfactsandbuiltinvars.html#the-factsfactname-hash this shows how you would access the value from the $facts hash with the nested keys of os and family.

if $facts['os']['family'] == 'redhat' {
  # ...

In your case, if you wanted to pull out the 'label' parameter from the above fact. (assuming you mean label when you say 'name') you would use the following command: $xvda1_label = $facts['xvda1']['label']

After seeing the discussion below, if what you are really after is the first 'key' used inside of the partition hash, you can simply do the following. This requires the stdlib module for access to the keys function

notify { "First key is ${facts['partitions'].keys[0]}": }

$ cat facts.pp 
$first_key_from_partitions = $facts['partitions'].keys[0]
notify { "First key is ${$first_key_from_partitions}": }
$ puppet apply facts.pp 
Notice: Compiled catalog for ubuntu in environment production in 0.13 seconds
Notice: First key is sda1
Notice: /Stage[main]/Main/Notify[First key is sda1]/message: defined 'message' as 'First key is sda1'
Notice: Applied catalog in 0.02 seconds

If you wished to select a specific label based on the mount point (in case of multiple partitions), you can use the filter function.

In my case, I only have /boot showing up in my partitions hash, and you can match the key used like this..

$ cat filter.pp 
$boot_partition_hash = $facts['partitions'].filter | $key, $value | {
  $value['mount'] == '/boot'
notify { "Key for mount at '/boot' is ${boot_partition_hash.keys[0]}": }
$ puppet apply filter.pp 
Notice: Compiled catalog for ubuntu in environment production in 0.10 seconds
Notice: Key for mount at '/boot' is sda1
Notice: /Stage[main]/Main/Notify[Key for mount at '/boot' is sda1]/message: defined 'message' as 'Key for mount at '/boot' is sda1'
Notice: Applied catalog in 0.02 seconds

If you wanted to match '/', you could do it like this

$root_partition_hash = $facts['partitions'].filter | $key, $value | {
  $value['mount'] == '/'
# Note that we are pulling out the keys array and getting the first value
notify { "Key for mount at '/' is ${root_partition_hash[0]}": }
edit flag offensive delete link more


But how would you get the key name xvda1 from the hash? That's what's being asked here.

lavaman gravatar imagelavaman ( 2017-01-12 16:33:55 -0500 )edit

I think he wants to know what the value of the label even if it changes. I have the same issue. I have a one-element hash and I need to put the label into a variable no matter what the label's value.

bschonecker gravatar imagebschonecker ( 2017-02-24 08:56:23 -0500 )edit

I'm not sure what the OP wants, if he want's the first key from the hash, even though it's not documented on https://docs.puppet.com/puppet/latest/function.html you can do `$first_key_from_partitions = $facts['partitions'].keys[0]`

DarylW gravatar imageDarylW ( 2017-02-24 09:49:46 -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



Asked: 2017-01-12 01:34:21 -0500

Seen: 722 times

Last updated: Feb 27 '17