puppetfacter

Puppet get root partition name using facter


I am trying to get a root partition (mount => "/") name using Puppet facter. When I run "facter partitions", it shows three partitions. I would like to get the variable "sda3" from the result.

{
  /dev/sda1 => {
    size => "1.00 MiB",
    size_bytes => 1048576
  },
  /dev/sda2 => {
    filesystem => "ext2",
    mount => "/boot",
    size => "477.00 MiB",
    size_bytes => 500170752,
    uuid => "8345d60e-e09a-4723-b5a6-149f4002706d"
  },
  /dev/sda3 => {
    filesystem => "ext4",
    mount => "/",
    size => "49.71 GiB",
    size_bytes => 53376712704,
    uuid => "a1330fb2-7f13-417e-8908-813b1fdae911"
  },

I tried $hddname = $facts['partitions']['mount'] == "/", but got an error. Do you guys have any idea?


Solution

  • When dealing with hashes and arrays, you probably want the Puppet Iteration and loops features. That page links to a few useful functions and will help you find which you need.

    First you need to filter the input:

    $root_partition = $facts['partitions'].filter |$device, $partition| { $partition['mount'] == '/' }
    

    This will assign the entire /dev/sda3 hash to $root_partition, equivalent to {"/dev/sda3" => {"filesystem" .... }}.

    Then extract the device name from the single hash key left using the keys function from stdlib:

    $hddname = keys($root_partition)[0]