gpt4 book ai didi

从 Puppet 中的 map 过滤键

转载 作者:行者123 更新时间:2023-12-01 15:25:00 25 4
gpt4 key购买 nike

我需要 puppet 中引用/datadisks 的挂载点。我使用 mountpoints core 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 的输出是一张 map ,如下所示:

{/=> {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, 容量=> 0.01%,设备 =>/dev/sdc1,文件系统 => ext4,选项 => [rw, seclabel, nosuid, nodev, noexec, noatime, nodiratime, data=ordered], size => 1006.82 GiB, size_bytes => 1081062445056,使用 => 76.02 MiB,使用_字节 => 79716352},/mnt/resource => {可用 => 110.06 GiB,可用_字节 => 118173499392,容量 => 0.05%,设备 =>/dev/sdb1,文件系统 => ext4 , options => [rw, seclabel, relatime, data=ordered], size => 110.12 GiB, size_bytes => 118236442624, used => 60.03 MiB, used_bytes => 62943232}}

最佳答案

我不是 100% 确定您的意思是只需要 key 还是 key 和数据(您说的代码有效似乎只是提取 key )。所以我会两者兼顾。

假设:

  $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/ }

获取 key 及其数据:

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

docs 中有一些很好的例子说明如何使用过滤器.

关于从 Puppet 中的 map 过滤键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com