puppetfacter

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?


Solution

  • The fastest way to solve this would be to use the keys function from puppetlabs/stdlib: https://forge.puppet.com/puppetlabs/stdlib/readme.

    keys() Returns the keys of a hash as an array. Type: rvalue.

    With that function, we can transform the output hash from Facter into an array of the keys and access its elements normally. Assuming that xvda1 is the 0th element,

    Facter 2:

    $variable = keys($::partitions)[0]

    Facter 3:

    $variable = keys($facts['partitions'])[0]