filterpuppetmount-pointfacter

Filter keys from a map in Puppet


I need the mount points in puppet that refer to /datadisks. I use mountpoints core facts to obtain the mount points. Within these mount points, I want to filter and get only the mount points whose names contain /datadisks in an array i.e I just want the keys that match the /datadisks pattern

The following works but I feel there's a better and more efficient way. Can someone please shed light on it?

$foo = flatten( map($::mountpoints) |$key,$value| { $key })
$filtered_data = $foo.filter |$items| { $items =~ /datadisks/ }

If I try the following, then it outputs the keys as well as values too. I just want the keys from the mountpoint map

$f_data = $::mountpoints.filter |$indexes, $values| { $indexes =~ /datadisks/ }

I'm on Openlogic CentOS 7.2 and the output of $::mountpoints is a map as below:

{/ => {available => 21.93 GiB, available_bytes => 23542669312, capacity => 26.88%, device => /dev/sda1, filesystem => xfs, options => [rw, seclabel, relatime, attr2, inode64, noquota], size => 29.98 GiB, size_bytes => 32195481600, used => 8.06 GiB, used_bytes => 8652812288}, /datadisks/disk2 => {available => 1006.74 GiB, available_bytes => 1080982728704, capacity => 0.01%, device => /dev/sdc1, filesystem => ext4, options => [rw, seclabel, nosuid, nodev, noexec, noatime, nodiratime, data=ordered], size => 1006.82 GiB, size_bytes => 1081062445056, used => 76.02 MiB, used_bytes => 79716352}, /mnt/resource => {available => 110.06 GiB, available_bytes => 118173499392, capacity => 0.05%, device => /dev/sdb1, filesystem => ext4, options => [rw, seclabel, relatime, data=ordered], size => 110.12 GiB, size_bytes => 118236442624, used => 60.03 MiB, used_bytes => 62943232}}


Solution

  • I am not 100% sure whether you mean you want just the keys or the keys and data (the code you say is working seems to be just extracting keys). So I'll do both.

    Assuming:

      $mountpoints = {
        '/' => {
          available => '21.93 GiB',
          available_bytes => '23542669312',
          capacity => '26.88%',
          device => '/dev/sda1',
          filesystem => 'xfs',
          options => ['rw', 'seclabel', 'relatime', 'attr2', 'inode64', 'noquota'],
          size => '29.98 GiB',
          size_bytes => '32195481600',
          used => '8.06 GiB',
          used_bytes => '8652812288',
        },
        '/datadisks/disk2' => {
          available => '1006.74 GiB',
          available_bytes => '1080982728704',
          capacity => '0.01%',
          device => '/dev/sdc1',
          filesystem => 'ext4',
          options => ['rw', 'seclabel', 'nosuid', 'nodev', 'noexec', 'noatime', 'nodiratime', 'data=ordered'],
          size => '1006.82 GiB',
          size_bytes => '1081062445056',
          used => '76.02 MiB',
          used_bytes => '79716352',
        },
        '/mnt/resource' => {
          available => '110.06 GiB',
          available_bytes => '118173499392',
          capacity => '0.05%',
          device => '/dev/sdb1',
          filesystem => 'ext4',
          options => ['rw', 'seclabel', 'relatime', 'data=ordered'],
          size => '110.12 GiB',
          size_bytes => '118236442624',
          used => '60.03 MiB',
          used_bytes => '62943232',
        },
      }
    

    To get just the keys that match the pattern /datadisks/:

    $datadisks = $mountpoints.keys.filter |$items| { $items =~ /datadisks/ }
    

    To get the keys and their data:

    $datadisks = $mountpoints.filter |$items| { $items[0] =~ /datadisks/ }
    

    There are some pretty good examples of how to use filter in the docs.