Chef infra 在哈希上的chef循环中,execute resource插值最后一个条目而不是当前条目

Chef infra 在哈希上的chef循环中,execute resource插值最后一个条目而不是当前条目,chef-infra,Chef Infra,我已经编写了一个创建多个逻辑卷的方法,作为安装应用程序设置的一部分。我通过读取包含驱动器所有元数据的JSON文件在配方中创建了一个哈希 配方在这个散列上循环,并且应该首先创建一个逻辑卷,然后立即执行tune2fs命令 现在假设我们在循环的第一次迭代中。它为散列中的第一个条目(lv_u01)创建逻辑卷,但当它通知执行块(在同一循环中,立即)时,它会以某种方式插入散列中最后一个条目(lv_u03)的键/值。我试了很多次,它一直在做同样的事情。我搜索了一下,找不到任何关于它为什么这样做的相关信息。这和

我已经编写了一个创建多个逻辑卷的方法,作为安装应用程序设置的一部分。我通过读取包含驱动器所有元数据的JSON文件在配方中创建了一个哈希

配方在这个散列上循环,并且应该首先创建一个逻辑卷,然后立即执行tune2fs命令

现在假设我们在循环的第一次迭代中。它为散列中的第一个条目(lv_u01)创建逻辑卷,但当它通知执行块(在同一循环中,立即)时,它会以某种方式插入散列中最后一个条目(lv_u03)的键/值。我试了很多次,它一直在做同样的事情。我搜索了一下,找不到任何关于它为什么这样做的相关信息。这和计时器有关吗?我希望在创建逻辑卷之后立即运行tune2fs命令,就像稍后在循环中装载卷一样,这一步骤需要在其他步骤运行之前执行。这就是为什么我立即使用

有人能帮我理解我做错了什么吗

以下是我看到的错误:

* lvm_logical_volume[lv_u01] action create

* execute[Run tune2fs] action run

================================================================================
Error executing action `run` on resource 'execute[Run tune2fs]'
================================================================================

           Mixlib::ShellOut::ShellCommandFailed
           ------------------------------------
           Expected process to exit with [0], but received '1'
           ---- Begin output of tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 ----
           STDOUT: tune2fs 1.45.4 (23-Sep-2019)
           STDERR: tune2fs: No such file or directory while trying to open /dev/my_vg/lv_u03
           Couldn't find valid filesystem superblock.
           ---- End output of tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 ----
           Ran tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 returned 1

           Resource Declaration:
           ---------------------
           # In /tmp/kitchen/cache/cookbooks/cookbook_name/recipes/recipe.rb

            88:     execute 'Run tune2fs' do
            89:         command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
            90:         action :nothing
            91:     end
            92:

           Compiled Resource:
           ------------------
           # Declared in /tmp/kitchen/cache/cookbooks/cookbook_name/recipes/recipe.rb:88:in `block in from_file'

           execute("Run tune2fs") do
             action [:nothing]
             default_guard_interpreter :execute
             command "tune2fs -i 0 -c 0 /dev/my_vg/lv_u03"
             backup 5
             declared_type :execute
             cookbook_name "cookbook_name"
             recipe_name "recipe_name"
             domain nil
             user nil
           end

           System Info:
           ------------
           chef_version=14.14.25
           platform=centos
           platform_version=8.2.2004
           ruby=ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux]
           program_name=/bin/chef-client
           executable=/opt/chef/bin/chef-client

代码如下:

recipe.rb

# Read metadata about drives into a hash
require 'json'
json_file = File.open 'lv_metadata.json'
lv_hash = JSON.load json_file
json_file.close

# Create and format logical volumes.  
    lv_hash.each do |lv_name, val_hash|

        # This resource will create the logical volume based on the parameters available in lv_hash and then format it
        # If sizing is static, use the fixed value else use 100% of the free space
        lvm_logical_volume lv_name do
            group val_hash['vg_name']
            size lazy {val_hash['sizing'] == 'static' ? "#{val_hash['disk_sizes'][app_env][vm_size]}G" : "100%FREE"}
            filesystem 'ext3'
            filesystem_params lazy {lv_name == 'lv_u02' ? "-m 0 -b #{val_hash['block_size']} -J size=256" : "-m 0 -b #{val_hash['block_size']}"}
            notifies :run, "execute[Run tune2fs]", :immediately
        end

        # Runs tune2fs to disable time-dependent checking. 
        # When setting max-mount-count to 0, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel.
        execute 'Run tune2fs' do
            command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
            action :nothing
        end

        # Create the directory where the logical volume will be mounted. Give it a
        directory "#{val_hash['filesystem_name']}" do
            owner val_hash['owner_name']
            group val_hash['group_name']
            mode val_hash['permissions']
            recursive true
            action :create
        end

        # Mount the logical volume at the specified filesystem path and enable it to add entry to fstab.
        mount "#{val_hash['filesystem_name']}" do
            device "/dev/#{val_hash['vg_name']}/#{lv_name}"
            fstype 'ext3'
            options val_hash['options']
            dump 0
            pass 0
            action [:mount, :enable]
        end

    end   
lv_metadata.json

{
    "lv_u01": {
        "vg_name": "vg_username",
        "filesystem_name": "/u01",
        "owner_name": "username",
        "group_name": "groupname",
        "permissions": 755,
        "options": "defaults",
        "sizing": "static",
        "disk_sizes": {
            "dev": {
                "small": 50,
                "medium": 50,
                "large": 50
            },
            "qa": {
                "small": 100,
                "medium": 100,
                "large": 100
            },
            "prod": {
                "small": 200,
                "medium": 200,
                "large": 200
            }
        },
        "block_size": 4096
    },
    "lv_u02": {
        "vg_name": "my_vg",
        "filesystem_name": "/u02",
        "owner_name": "username",
        "group_name": "groupname",
        "permissions": 755,
        "options": "defaults",
        "sizing": "static",
        "disk_sizes": {
            "dev": {
                "small": 5,
                "medium": 5,
                "large": 5
            },
            "qa": {
                "small": 8,
                "medium": 8,
                "large": 100
            },
            "prod": {
                "small": 10,
                "medium": 10,
                "large": 200
            }
        },
        "block_size": 4096
    },
    "lv_u03": {
        "vg_name": "my_vg",
        "filesystem_name": "/u03",
        "owner_name": "username",
        "group_name": "groupname",
        "permissions": 755,
        "options": "defaults",
        "sizing": "free",
        "disk_sizes": {
            "dev": {
                "small": 5,
                "medium": 5,
                "large": 5
            },
            "qa": {
                "small": 5,
                "medium": 5,
                "large": 5
            },
            "prod": {
                "small": 5,
                "medium": 5,
                "large": 5
            }
        },
        "block_size": 1024
    }
    
}


您不应该使用相同名称的资源,它们实际上做不同的事情。资源集合中最终有3个同名的资源:
execute'Run tune2fs'
,但每个资源都在做自己的事情,因为
命令“tune2fs-i 0-c0/dev/#{val_hash['vg_name']}/#{lv u name}
是不同的

厨师长只是不知道您正在通知3中的哪一个资源

通知:运行,“执行[run tune2fs]”,立即执行

为您的执行资源使用不同的名称

lvm\u逻辑卷lv\u名称do
组值散列['vg\U名称']
大小延迟{val_hash['sizing']=='static'?“{val_hash['disk_size']][app_env][vm_size]}G:“100%免费”}
文件系统“ext3”
文件系统_参数lazy{lv_name=='lv_u02'?“-m0-b{val_hash['block_size']}-J size=256:“-m0-b{val_hash['block_size']}”
通知:运行,“立即执行[run tune2fs for#{lv_name}]”
结束
#运行tune2fs以禁用时间相关检查。
#将max mount count设置为0时,e2fsck(8)和内核将忽略文件系统的装载次数。
执行“为#{lv_name}运行tune2fs”do
命令“tune2fs-i0-c0/dev/#{val_hash['vg_name']}/#{lv_name}”
行动:没什么
结束

非常感谢,德拉科!这是有道理的。不知道我怎么会完全忽略了这么明显的事情。大脑以神秘的方式运作。