Filter keys from map in Puppet

I need the mount points in the puppet that refer to / datadisks. I am using mountpoints

core facts

to get mount points. Within these mount points, I want to filter and get only mount points whose names contain / datadisks in the array. I just want keys matching pattern / datadisks

The following works, but I feel that it is better and more efficient there. Can anyone shed some 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 keys as well as values. I just need the keys from the mountpoint

map

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

      

I am on Openlogic CentOS 7.2 and the output $::mountpoints

is a map as shown 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}}

+3


source to share


1 answer


I'm not 100% sure if you mean you only want keys or keys and data (the code you say seems to just extract keys). So I will do both.

Assuming that:

  $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 only the keys that match the pattern /datadisks/

:



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

      

To get keys and their data:

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

      

There are some good examples of filter usage in the docs .

+2


source







All Articles