来自木偶中地图的过滤键



我需要木偶中的安装点,这些安装点可以参考/datadisks。我使用mountpoints核心facts获得安装点。在这些安装点中,我要过滤并仅获取数组中包含/datAdisks的安装点,即我只想要匹配/datadisks模式的键

以下作用,但我觉得有一种更好,更有效的方法。有人可以阐明它吗?

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

如果我尝试以下内容,则它也输出键以及值。我只想要mountpoint地图的键

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

我在OpenLogic CentOS 7.2上,$::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}}

我不是100%确定您是否只是只需键或键和数据(您所说的代码工作似乎只是在提取键(。所以我会同时做。

假设:

  $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',
    },
  }

仅获取匹配模式/datadisks/的键:

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

获取键及其数据:

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

有一些很好的例子,说明了如何在文档中使用过滤器。

相关内容

  • 没有找到相关文章

最新更新